From b0a61cce42c9c97f691ff6ed1967ecc1b382a62a Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Sat, 27 May 2023 21:42:12 +0100 Subject: [PATCH] Added failing test (a bit dirty using a spy for the actual "system under test" but it is the quickest way to test added functionality and mocking out complexity around coroutines and httpclient) --- .../amethyst/service/Nip05VerifierTest.kt | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt diff --git a/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt new file mode 100644 index 000000000..52f8e5c6e --- /dev/null +++ b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt @@ -0,0 +1,93 @@ +package com.vitorpamplona.amethyst.service + +import io.mockk.MockKAnnotations +import io.mockk.every +import io.mockk.impl.annotations.SpyK +import io.mockk.unmockkAll +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.fail +import org.junit.Before +import org.junit.Test + +class Nip05VerifierTest { + private val ALL_UPPER_CASE_USER_NAME = "ONETWO" + private val ALL_LOWER_CASE_USER_NAME = "onetwo" + + @SpyK + var nip05Verifier = Nip05Verifier() + + @Before + fun setUp() = MockKAnnotations.init(this) + + @Test + fun `test with matching case on user name`() { + // Set-up + val userNameToTest = ALL_UPPER_CASE_USER_NAME + val expectedPubKey = "ca29c211f1c72d5b6622268ff43d2288ea2b2cb5b9aa196ff9f1704fc914b71b" + + val nostrJson = "{\n" + + " \"names\": {\n" + + " \"$userNameToTest\": \"$expectedPubKey\" \n" + + " }\n" + + "}" + + every { nip05Verifier.fetchNip05Json(any(), any(), any()) } answers { + secondArg<(String) -> Unit>().invoke(nostrJson) + } + + val nip05 = "$userNameToTest@domain.com" + var actualPubkeyHex = "" + + // Execution + nip05Verifier.verifyNip05( + nip05, + onSuccess = { + actualPubkeyHex = it + }, + onError = { + fail("Test failure") + } + ) + + // Verification + assertEquals(expectedPubKey, actualPubkeyHex) + } + + @Test + fun `test with NOT matching case on user name`() { + // Set-up + val expectedPubKey = "ca29c211f1c72d5b6622268ff43d2288ea2b2cb5b9aa196ff9f1704fc914b71b" + + val nostrJson = "{\n" + + " \"names\": {\n" + + " \"$ALL_UPPER_CASE_USER_NAME\": \"$expectedPubKey\" \n" + + " }\n" + + "}" + every { nip05Verifier.fetchNip05Json(any(), any(), any()) } answers { + secondArg<(String) -> Unit>().invoke(nostrJson) + } + + val nip05 = "$ALL_LOWER_CASE_USER_NAME@domain.com" + var actualPubkeyHex = "" + + // Execution + nip05Verifier.verifyNip05( + nip05, + onSuccess = { + actualPubkeyHex = it + }, + onError = { + fail("Test failure") + } + ) + + // Verification + assertEquals(expectedPubKey, actualPubkeyHex) + } + + @After + fun tearDown() { + unmockkAll() + } +}