@@ -8,8 +8,11 @@ import id.passage.android.IntegrationTestConfig.Companion.API_BASE_URL
8
8
import id.passage.android.IntegrationTestConfig.Companion.APP_ID_OTP
9
9
import id.passage.android.IntegrationTestConfig.Companion.EXISTING_USER_EMAIL_OTP
10
10
import id.passage.android.IntegrationTestConfig.Companion.WAIT_TIME_MILLISECONDS
11
+ import id.passage.android.exceptions.PassageTokenException
11
12
import id.passage.android.exceptions.PassageUserUnauthorizedException
12
13
import id.passage.android.exceptions.UserInfoUnauthorizedException
14
+ import id.passage.android.model.AuthResult
15
+ import junit.framework.TestCase.assertNotNull
13
16
import junit.framework.TestCase.fail
14
17
import kotlinx.coroutines.delay
15
18
import kotlinx.coroutines.runBlocking
@@ -24,6 +27,7 @@ import org.junit.runner.RunWith
24
27
@RunWith(AndroidJUnit4 ::class )
25
28
internal class TokenStoreTests {
26
29
private lateinit var passage: Passage
30
+ private var refreshToken = " "
27
31
28
32
@Before
29
33
fun setup (): Unit =
@@ -38,7 +42,8 @@ internal class TokenStoreTests {
38
42
val otpId = passage.oneTimePasscode.login(EXISTING_USER_EMAIL_OTP ).otpId
39
43
delay(WAIT_TIME_MILLISECONDS )
40
44
val otp = MailosaurAPIClient .getMostRecentOneTimePasscode()
41
- passage.oneTimePasscode.activate(otp, otpId)
45
+ val authResult = passage.oneTimePasscode.activate(otp, otpId)
46
+ refreshToken = authResult.authToken
42
47
}
43
48
44
49
@After
@@ -111,4 +116,57 @@ internal class TokenStoreTests {
111
116
assertThat(e is PassageUserUnauthorizedException )
112
117
}
113
118
}
119
+
120
+ @Test
121
+ fun getValidAuthTokenWithValidToken () =
122
+ runTest {
123
+ val result = passage.tokenStore.getValidAuthToken()
124
+ assertNotNull(result)
125
+ }
126
+
127
+ @Test
128
+ fun getValidAuthTokenWithInvalidToken () =
129
+ runTest {
130
+ try {
131
+ passage.tokenStore.setTokens(AuthResult (" invalid" , " " ))
132
+ passage.tokenStore.getValidAuthToken()
133
+ fail(" Test should throw PassageTokenException" )
134
+ } catch (e: Exception ) {
135
+ assertThat(e is PassageTokenException )
136
+ }
137
+ }
138
+
139
+ @Test
140
+ fun isAuthTokenValidWithValidToken () {
141
+ val validToken = IntegrationTestConfig .AUTH_TOEKN
142
+ val result = passage.tokenStore.isAuthTokenValid(validToken)
143
+ assertThat(result).isTrue()
144
+ }
145
+
146
+ @Test
147
+ fun isAuthTokenValidWithInvalidToken () {
148
+ val invalidToken = " invalidAuthToken"
149
+ val result = passage.tokenStore.isAuthTokenValid(invalidToken)
150
+ assertThat(result).isFalse()
151
+ }
152
+
153
+ @Test
154
+ fun revokedWithValidToken () =
155
+ runTest {
156
+ val validRefreshToken = refreshToken
157
+ passage.tokenStore.revokeRefreshToken(validRefreshToken)
158
+ // should not throw any error
159
+ }
160
+
161
+ @Test
162
+ fun refreshWithInvalidToken (): Unit =
163
+ runBlocking {
164
+ try {
165
+ val invalidRefreshToken = " invalid"
166
+ val authResult = passage.tokenStore.refreshAuthToken(invalidRefreshToken)
167
+ fail(" Test should throw PassageTokenException" )
168
+ } catch (e: Exception ) {
169
+ assertThat(e is PassageTokenException )
170
+ }
171
+ }
114
172
}
0 commit comments