|
| 1 | +package org.wordpress.android.util.publicdata |
| 2 | + |
| 3 | +import com.nhaarman.mockitokotlin2.mock |
| 4 | +import com.nhaarman.mockitokotlin2.verify |
| 5 | +import com.nhaarman.mockitokotlin2.whenever |
| 6 | +import org.junit.Assert |
| 7 | +import org.junit.Before |
| 8 | +import org.junit.Test |
| 9 | +import org.wordpress.android.util.signature.SignatureUtils |
| 10 | +import org.wordpress.android.viewmodel.ContextProvider |
| 11 | + |
| 12 | +class ClientVerificationTest { |
| 13 | + private val jetpackPublicData: JetpackPublicData = mock() |
| 14 | + private val signatureUtils: SignatureUtils = mock() |
| 15 | + private val contextProvider: ContextProvider = mock() |
| 16 | + private val classToTest = ClientVerification(jetpackPublicData, signatureUtils, contextProvider) |
| 17 | + |
| 18 | + private val expectedPackage = "match" |
| 19 | + private val expectedSignatureHash = "signatureHash" |
| 20 | + |
| 21 | + @Before |
| 22 | + fun setup() { |
| 23 | + whenever(jetpackPublicData.currentPackageId()).thenReturn(expectedPackage) |
| 24 | + whenever(jetpackPublicData.currentPublicKeyHash()).thenReturn(expectedSignatureHash) |
| 25 | + whenever(signatureUtils.getSignatureHash(contextProvider.getContext(), expectedPackage)) |
| 26 | + .thenReturn(expectedSignatureHash) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `Should return false if calling package is null when canTrust is called`() { |
| 31 | + val expected = false |
| 32 | + val actual = classToTest.canTrust(null) |
| 33 | + Assert.assertEquals(expected, actual) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `Should return false if caller package does not match the expected when canTrust is called`() { |
| 38 | + val expected = false |
| 39 | + val actual = classToTest.canTrust("no_match") |
| 40 | + Assert.assertEquals(expected, actual) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `Should return false if caller signature hash does not match the expected when canTrust is called`() { |
| 45 | + whenever(signatureUtils.getSignatureHash(contextProvider.getContext(), expectedPackage)) |
| 46 | + .thenReturn("no_match") |
| 47 | + val expected = false |
| 48 | + val actual = classToTest.canTrust(expectedPackage) |
| 49 | + Assert.assertEquals(expected, actual) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `Should return true if caller package and signature hash match the expected when canTrust is called`() { |
| 54 | + val expected = true |
| 55 | + val actual = classToTest.canTrust(expectedPackage) |
| 56 | + Assert.assertEquals(expected, actual) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `Should call jetpackPublicData currentPackageId when canTrust is called`() { |
| 61 | + classToTest.canTrust(expectedPackage) |
| 62 | + verify(jetpackPublicData).currentPackageId() |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `Should call signatureUtils getSignatureHash when canTrust is called`() { |
| 67 | + classToTest.canTrust(expectedPackage) |
| 68 | + verify(signatureUtils).getSignatureHash(contextProvider.getContext(), expectedPackage) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun `Should call jetpackPublicData currentPublicKeyHash when canTrust is called`() { |
| 73 | + classToTest.canTrust(expectedPackage) |
| 74 | + verify(jetpackPublicData).currentPublicKeyHash() |
| 75 | + } |
| 76 | +} |
0 commit comments