27
27
import java .nio .file .FileSystem ;
28
28
import java .nio .file .Files ;
29
29
import java .nio .file .Path ;
30
+ import java .time .Instant ;
31
+ import java .time .ZoneOffset ;
32
+ import java .time .ZonedDateTime ;
33
+ import java .time .format .DateTimeFormatter ;
30
34
import org .junit .jupiter .api .Test ;
31
35
32
36
/**
@@ -40,7 +44,7 @@ public class SsoAccessTokenProviderTest {
40
44
private static final String WRONG_TOKEN_FILE_NAME = "wrong-token-file.json" ;
41
45
42
46
@ Test
43
- public void cachedTokenFile_correctFormat_resolveAccessTokenCorrectly () throws IOException {
47
+ void cachedTokenFile_correctFormat_resolveAccessTokenCorrectly () throws IOException {
44
48
String tokenFile = "{\n " +
45
49
"\" accessToken\" : \" base64string\" ,\n " +
46
50
"\" expiresAt\" : \" 2090-01-01T00:00:00Z\" ,\n " +
@@ -53,7 +57,7 @@ public void cachedTokenFile_correctFormat_resolveAccessTokenCorrectly() throws I
53
57
}
54
58
55
59
@ Test
56
- public void cachedTokenFile_accessTokenMissing_throwNullPointerException () throws IOException {
60
+ void cachedTokenFile_accessTokenMissing_throwNullPointerException () throws IOException {
57
61
String tokenFile = "{\n " +
58
62
"\" expiresAt\" : \" 2090-01-01T00:00:00Z\" ,\n " +
59
63
"\" region\" : \" us-west-2\" , \n " +
@@ -65,7 +69,7 @@ public void cachedTokenFile_accessTokenMissing_throwNullPointerException() throw
65
69
}
66
70
67
71
@ Test
68
- public void cachedTokenFile_expiresAtMissing_throwNullPointerException () throws IOException {
72
+ void cachedTokenFile_expiresAtMissing_throwNullPointerException () throws IOException {
69
73
String tokenFile = "{\n " +
70
74
"\" accessToken\" : \" base64string\" ,\n " +
71
75
"\" region\" : \" us-west-2\" , \n " +
@@ -78,7 +82,7 @@ public void cachedTokenFile_expiresAtMissing_throwNullPointerException() throws
78
82
}
79
83
80
84
@ Test
81
- public void cachedTokenFile_optionalRegionMissing_resolveAccessTokenCorrectly () throws IOException {
85
+ void cachedTokenFile_optionalRegionMissing_resolveAccessTokenCorrectly () throws IOException {
82
86
String tokenFile = "{\n " +
83
87
"\" accessToken\" : \" base64string\" ,\n " +
84
88
"\" expiresAt\" : \" 2090-01-01T00:00:00Z\" ,\n " +
@@ -90,7 +94,7 @@ public void cachedTokenFile_optionalRegionMissing_resolveAccessTokenCorrectly()
90
94
}
91
95
92
96
@ Test
93
- public void cachedTokenFile_optionalStartUrlMissing_resolveAccessTokenCorrectly () throws IOException {
97
+ void cachedTokenFile_optionalStartUrlMissing_resolveAccessTokenCorrectly () throws IOException {
94
98
String tokenFile = "{\n " +
95
99
"\" accessToken\" : \" base64string\" ,\n " +
96
100
"\" expiresAt\" : \" 2090-01-01T00:00:00Z\" ,\n " +
@@ -102,7 +106,7 @@ public void cachedTokenFile_optionalStartUrlMissing_resolveAccessTokenCorrectly(
102
106
}
103
107
104
108
@ Test
105
- public void cachedTokenFile_alreadyExpired_resolveAccessTokenCorrectly () throws IOException {
109
+ void cachedTokenFile_alreadyExpired_resolveAccessTokenCorrectly () throws IOException {
106
110
String tokenFile = "{\n " +
107
111
"\" accessToken\" : \" base64string\" ,\n " +
108
112
"\" expiresAt\" : \" 2019-01-01T00:00:00Z\" ,\n " +
@@ -115,7 +119,7 @@ public void cachedTokenFile_alreadyExpired_resolveAccessTokenCorrectly() throws
115
119
}
116
120
117
121
@ Test
118
- public void cachedTokenFile_tokenFileNotExist_throwNullPointerException () throws IOException {
122
+ void cachedTokenFile_tokenFileNotExist_throwNullPointerException () throws IOException {
119
123
String tokenFile = "{\n " +
120
124
"\" accessToken\" : \" base64string\" ,\n " +
121
125
"\" expiresAt\" : \" 2019-01-01T00:00:00Z\" ,\n " +
@@ -127,6 +131,44 @@ public void cachedTokenFile_tokenFileNotExist_throwNullPointerException() throws
127
131
assertThatThrownBy (() -> provider .resolveToken ().token ()).isInstanceOf (UncheckedIOException .class );
128
132
}
129
133
134
+ @ Test
135
+ void cachedTokenFile_AboutToExpire_resolveAccessTokenCorrectly () throws IOException {
136
+ String tokenFile = String .format ("{\n " +
137
+ "\" accessToken\" : \" base64string\" ,\n " +
138
+ "\" expiresAt\" : \" %s\" ,\n " +
139
+ "\" startUrl\" : \" " + START_URL +"\" \n " +
140
+ "}" , stringFormattedTime (Instant .now ().plusSeconds (10 )));
141
+ SsoAccessTokenProvider provider = new SsoAccessTokenProvider (
142
+ prepareTestCachedTokenFile (tokenFile , GENERATED_TOKEN_FILE_NAME ));
143
+ assertThat (provider .resolveToken ().token ()).isEqualTo ("base64string" );
144
+ }
145
+
146
+ @ Test
147
+ void cachedTokenFile_JustExpired_throwsExpiredTokenException () throws IOException {
148
+ String tokenFile = String .format ("{\n " +
149
+ "\" accessToken\" : \" base64string\" ,\n " +
150
+ "\" expiresAt\" : \" %s\" ,\n " +
151
+ "\" startUrl\" : \" " + START_URL +"\" \n " +
152
+ "}" , stringFormattedTime (Instant .now ()));
153
+ SsoAccessTokenProvider provider = new SsoAccessTokenProvider (
154
+ prepareTestCachedTokenFile (tokenFile , GENERATED_TOKEN_FILE_NAME ));
155
+ assertThatThrownBy (() -> provider .resolveToken ().token ()).hasMessageContaining ("The SSO session associated with this profile "
156
+ + "has expired or is otherwise invalid." );
157
+ }
158
+
159
+ @ Test
160
+ void cachedTokenFile_ExpiredFewSecondsAgo_throwsExpiredTokenException () throws IOException {
161
+ String tokenFile = String .format ("{\n " +
162
+ "\" accessToken\" : \" base64string\" ,\n " +
163
+ "\" expiresAt\" : \" %s\" ,\n " +
164
+ "\" startUrl\" : \" " + START_URL +"\" \n " +
165
+ "}" , stringFormattedTime (Instant .now ().minusSeconds (1 )));
166
+ SsoAccessTokenProvider provider = new SsoAccessTokenProvider (
167
+ prepareTestCachedTokenFile (tokenFile , GENERATED_TOKEN_FILE_NAME ));
168
+ assertThatThrownBy (() -> provider .resolveToken ().token ()).hasMessageContaining ("The SSO session associated with this profile "
169
+ + "has expired or is otherwise invalid." );
170
+ }
171
+
130
172
private Path prepareTestCachedTokenFile (String tokenFileContent , String generatedTokenFileName ) throws IOException {
131
173
FileSystem fs = Jimfs .newFileSystem (Configuration .unix ());
132
174
Path fileDirectory = fs .getPath ("./foo" );
@@ -142,4 +184,11 @@ private Path createTestCachedTokenFilePath(Path directory, String tokenFileName)
142
184
return directory .resolve (tokenFileName );
143
185
}
144
186
187
+ private String stringFormattedTime (Instant instant ){
188
+ // Convert Instant to ZonedDateTime with UTC time zone
189
+ ZonedDateTime zonedDateTime = instant .atZone (ZoneOffset .UTC );
190
+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss'Z'" );
191
+ return formatter .format (zonedDateTime );
192
+ }
193
+
145
194
}
0 commit comments