Skip to content

Commit 1c7cc60

Browse files
author
Jan Phillip Kretzschmar
committed
test: add test for OnepasswordAccessCredentials
1 parent 14f13eb commit 1c7cc60

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ dependencies {
3131

3232
testImplementation("junit:junit:4.13.2")
3333
testImplementation(gradleTestKit())
34+
testImplementation("io.mockk:mockk:1.12.3")
3435
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.valtechmobility.gradle.credentials.onepassword
2+
3+
import io.mockk.every
4+
import io.mockk.mockk
5+
import io.mockk.mockkStatic
6+
import io.mockk.unmockkAll
7+
import io.mockk.verify
8+
import org.junit.After
9+
import org.junit.Before
10+
import org.junit.Test
11+
12+
/**
13+
* Example local unit test, which will execute on the development machine (host).
14+
*
15+
* See [testing documentation](http://d.android.com/tools/testing).
16+
*/
17+
internal class OnepasswordAccessCredentialsTest {
18+
19+
private lateinit var credentials: OnepasswordAccessCredentials
20+
21+
@Before
22+
fun setup() {
23+
credentials = OnepasswordAccessCredentials(EXAMPLE_VAULT_KEY)
24+
mockkStatic(Runtime::getRuntime)
25+
every { Runtime.getRuntime().exec(any<String>()) } returns mockk {
26+
every { waitFor() } returns 0
27+
every { inputStream } returns EXAMPLE_RESULT.byteInputStream()
28+
every { destroyForcibly() } returns this
29+
}
30+
}
31+
32+
@After
33+
fun teardown() {
34+
unmockkAll()
35+
}
36+
37+
@Test
38+
fun credentialsCreated_usernameRequested_usernameQueriedFromOP() {
39+
credentials.username
40+
verify {
41+
Runtime.getRuntime()
42+
.exec("op item get \"$EXAMPLE_VAULT_KEY\" --fields label=username")
43+
}
44+
}
45+
46+
@Test
47+
fun credentialsCreated_passwordRequested_passwordQueriedFromOP() {
48+
credentials.password
49+
verify {
50+
Runtime.getRuntime()
51+
.exec("op item get \"$EXAMPLE_VAULT_KEY\" --fields label=password")
52+
}
53+
}
54+
55+
@Test
56+
fun credentialsCreated_usernameRequestedMultipleTimes_usernameQueriedFromOPOnce() {
57+
credentials.username
58+
credentials.username
59+
verify(exactly = 1) {
60+
Runtime.getRuntime()
61+
.exec("op item get \"$EXAMPLE_VAULT_KEY\" --fields label=username")
62+
}
63+
}
64+
65+
@Test
66+
fun credentialsCreated_passwordRequestedMultipleTimes_passwordQueriedFromOPOnce() {
67+
credentials.password
68+
credentials.password
69+
verify(exactly = 1) {
70+
Runtime.getRuntime()
71+
.exec("op item get \"$EXAMPLE_VAULT_KEY\" --fields label=password")
72+
}
73+
}
74+
75+
companion object {
76+
private const val EXAMPLE_VAULT_KEY = "EXAMPLE_VAULT_KEY"
77+
private const val EXAMPLE_RESULT = "EXAMPLE_RESULT"
78+
}
79+
}

0 commit comments

Comments
 (0)