From b0a61cce42c9c97f691ff6ed1967ecc1b382a62a Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Sat, 27 May 2023 21:42:12 +0100 Subject: [PATCH 1/4] 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() + } +} From aa0a4d1aaca169d8833b9d39161c1f3d2f555438 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Sat, 27 May 2023 21:56:57 +0100 Subject: [PATCH 2/4] Use lowercase versions of user name and retrieved json to make case insensitive match --- .../java/com/vitorpamplona/amethyst/service/Nip05Verifier.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/Nip05Verifier.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/Nip05Verifier.kt index e86a85633..8eb63b2d8 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/Nip05Verifier.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/Nip05Verifier.kt @@ -76,13 +76,13 @@ class Nip05Verifier() { nip05, onSuccess = { val nip05url = try { - mapper.readTree(it) + mapper.readTree(it.lowercase()) } catch (t: Throwable) { onError("Error Parsing JSON from Lightning Address. Check the user's lightning setup") null } - val user = nip05.split("@")[0] + val user = nip05.split("@")[0].lowercase() val hexKey = nip05url?.get("names")?.get(user)?.asText() From 88bad1e30895d3cbd2a4f922804cc2b2c6ef503b Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Sat, 27 May 2023 22:35:34 +0100 Subject: [PATCH 3/4] added additional trivial tests --- .../amethyst/service/Nip05VerifierTest.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt index 52f8e5c6e..44d964ec8 100644 --- a/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt +++ b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt @@ -6,6 +6,7 @@ import io.mockk.impl.annotations.SpyK import io.mockk.unmockkAll import org.junit.After import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Assert.fail import org.junit.Before import org.junit.Test @@ -90,4 +91,31 @@ class Nip05VerifierTest { fun tearDown() { unmockkAll() } + + @Test + fun `test assmble url with invalid value returns null`() { + // given + val nip05address = "this@that@that.com" + + // when + val actualValue = nip05Verifier.assembleUrl(nip05address) + + // then + assertNull(actualValue) + } + + @Test + fun `test assmble url with valid value returns user url`() { + // given + val userName = "TheUser" + val domain = "domain.com" + val nip05address = "$userName@$domain" + val expectedValue = "https://$domain/.well-known/nostr.json?name=$userName" + + // when + val actualValue = nip05Verifier.assembleUrl(nip05address) + + // then + assertEquals(expectedValue, actualValue) + } } From 7f6b44cd0b91e23fe9e1101f55914d0d6c5a5ea4 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Sat, 27 May 2023 23:50:22 +0100 Subject: [PATCH 4/4] spelling fix --- .../com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt index 44d964ec8..afd6107de 100644 --- a/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt +++ b/app/src/test/java/com/vitorpamplona/amethyst/service/Nip05VerifierTest.kt @@ -93,7 +93,7 @@ class Nip05VerifierTest { } @Test - fun `test assmble url with invalid value returns null`() { + fun `execute assemble url with invalid value returns null`() { // given val nip05address = "this@that@that.com" @@ -105,7 +105,7 @@ class Nip05VerifierTest { } @Test - fun `test assmble url with valid value returns user url`() { + fun `execute assemble url with valid value returns nip05 url`() { // given val userName = "TheUser" val domain = "domain.com"