@@ -18,6 +18,96 @@ describe("InstallationTokenCache", () => {
18
18
jest . useRealTimers ( ) ;
19
19
} ) ;
20
20
21
+ it ( "Reuse same token for cloud installation" , async ( ) => {
22
+
23
+ const GITHUB_INSTALLATION_ID = 1 ;
24
+ jest . setSystemTime ( now ) ;
25
+ const token1 = new AuthToken ( "token1" , in10Minutes ) ;
26
+ const token2 = new AuthToken ( "token2" , in10Minutes ) ;
27
+
28
+ const cache1 = InstallationTokenCache . getInstance ( ) ;
29
+ const cache2 = InstallationTokenCache . getInstance ( ) ;
30
+
31
+ const foundToken1 = await cache1 . getInstallationToken ( GITHUB_INSTALLATION_ID , undefined , ( ) => Promise . resolve ( token1 ) ) ;
32
+ const foundToken2 = await cache2 . getInstallationToken ( GITHUB_INSTALLATION_ID , undefined , ( ) => Promise . resolve ( token2 ) ) ;
33
+
34
+ expect ( foundToken1 ) . toEqual ( foundToken2 ) ;
35
+
36
+ } ) ;
37
+
38
+ it ( "Reuse same token for GHE installation" , async ( ) => {
39
+
40
+ const GITHUB_INSTALLATION_ID = 1 ;
41
+ const GITHUB_APP_ID = 1 ;
42
+ jest . setSystemTime ( now ) ;
43
+ const token1 = new AuthToken ( "token1" , in10Minutes ) ;
44
+ const token2 = new AuthToken ( "token2" , in10Minutes ) ;
45
+
46
+ const cache1 = InstallationTokenCache . getInstance ( ) ;
47
+ const cache2 = InstallationTokenCache . getInstance ( ) ;
48
+
49
+ const foundToken1 = await cache1 . getInstallationToken ( GITHUB_INSTALLATION_ID , GITHUB_APP_ID , ( ) => Promise . resolve ( token1 ) ) ;
50
+ const foundToken2 = await cache2 . getInstallationToken ( GITHUB_INSTALLATION_ID , GITHUB_APP_ID , ( ) => Promise . resolve ( token2 ) ) ;
51
+
52
+ expect ( foundToken1 ) . toEqual ( foundToken2 ) ;
53
+
54
+ } ) ;
55
+
56
+ it ( "won't have conflicts on the token for cloud installations" , async ( ) => {
57
+
58
+ const GITHUB_INSTALLATION_ID_1 = 21 ;
59
+ const GITHUB_INSTALLATION_ID_2 = 22 ;
60
+ jest . setSystemTime ( now ) ;
61
+ const token1 = new AuthToken ( "token1" , in10Minutes ) ;
62
+ const token2 = new AuthToken ( "token2" , in10Minutes ) ;
63
+
64
+ const cache1 = InstallationTokenCache . getInstance ( ) ;
65
+ const cache2 = InstallationTokenCache . getInstance ( ) ;
66
+
67
+ const foundToken1 = await cache1 . getInstallationToken ( GITHUB_INSTALLATION_ID_1 , undefined , ( ) => Promise . resolve ( token1 ) ) ;
68
+ const foundToken2 = await cache2 . getInstallationToken ( GITHUB_INSTALLATION_ID_2 , undefined , ( ) => Promise . resolve ( token2 ) ) ;
69
+
70
+ expect ( foundToken1 ) . not . toEqual ( foundToken2 ) ;
71
+
72
+ } ) ;
73
+
74
+ it ( "won't have conflicts on the token for different GHE installations" , async ( ) => {
75
+
76
+ const CONFLICTIN_GITHUB_INSTALLATION_ID = 31 ;
77
+ const GITHUB_APP_ID_1 = 31 ;
78
+ const GITHUB_APP_ID_2 = 32 ;
79
+ jest . setSystemTime ( now ) ;
80
+ const token1 = new AuthToken ( "token1" , in10Minutes ) ;
81
+ const token2 = new AuthToken ( "token2" , in10Minutes ) ;
82
+
83
+ const cache1 = InstallationTokenCache . getInstance ( ) ;
84
+ const cache2 = InstallationTokenCache . getInstance ( ) ;
85
+
86
+ const foundToken1 = await cache1 . getInstallationToken ( CONFLICTIN_GITHUB_INSTALLATION_ID , GITHUB_APP_ID_1 , ( ) => Promise . resolve ( token1 ) ) ;
87
+ const foundToken2 = await cache2 . getInstallationToken ( CONFLICTIN_GITHUB_INSTALLATION_ID , GITHUB_APP_ID_2 , ( ) => Promise . resolve ( token2 ) ) ;
88
+
89
+ expect ( foundToken1 ) . not . toEqual ( foundToken2 ) ;
90
+
91
+ } ) ;
92
+
93
+ it ( "won't have conflicts on the token for different cloud and GHE installations" , async ( ) => {
94
+
95
+ const CONFLICTIN_GITHUB_INSTALLATION_ID = 41 ;
96
+ const GITHUB_APP_ID = 41 ;
97
+ jest . setSystemTime ( now ) ;
98
+ const token1 = new AuthToken ( "token1" , in10Minutes ) ;
99
+ const token2 = new AuthToken ( "token2" , in10Minutes ) ;
100
+
101
+ const cache1 = InstallationTokenCache . getInstance ( ) ;
102
+ const cache2 = InstallationTokenCache . getInstance ( ) ;
103
+
104
+ const foundToken1 = await cache1 . getInstallationToken ( CONFLICTIN_GITHUB_INSTALLATION_ID , undefined , ( ) => Promise . resolve ( token1 ) ) ;
105
+ const foundToken2 = await cache2 . getInstallationToken ( CONFLICTIN_GITHUB_INSTALLATION_ID , GITHUB_APP_ID , ( ) => Promise . resolve ( token2 ) ) ;
106
+
107
+ expect ( foundToken1 ) . not . toEqual ( foundToken2 ) ;
108
+
109
+ } ) ;
110
+
21
111
it ( "Re-generates expired tokens" , async ( ) => {
22
112
const initialInstallationToken = new AuthToken ( "initial installation token" , in10Minutes ) ;
23
113
const generateInitialInstallationToken = jest . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( initialInstallationToken ) ) ;
@@ -29,21 +119,21 @@ describe("InstallationTokenCache", () => {
29
119
const installationTokenCache = new InstallationTokenCache ( ) ;
30
120
31
121
jest . setSystemTime ( now ) ;
32
- const token1 = await installationTokenCache . getInstallationToken ( githubInstallationId , generateInitialInstallationToken ) ;
122
+ const token1 = await installationTokenCache . getInstallationToken ( githubInstallationId , undefined , generateInitialInstallationToken ) ;
33
123
expect ( token1 ) . toEqual ( initialInstallationToken ) ;
34
124
expect ( generateInitialInstallationToken ) . toHaveBeenCalledTimes ( 1 ) ;
35
125
expect ( generateFreshInstallationToken ) . toHaveBeenCalledTimes ( 0 ) ;
36
126
37
127
// after 5 minutes we still expect the same token because it's still valid
38
128
jest . setSystemTime ( in5Minutes ) ;
39
- const token2 = await installationTokenCache . getInstallationToken ( githubInstallationId , generateFreshInstallationToken ) ;
129
+ const token2 = await installationTokenCache . getInstallationToken ( githubInstallationId , undefined , generateFreshInstallationToken ) ;
40
130
expect ( token2 ) . toEqual ( initialInstallationToken ) ;
41
131
expect ( generateInitialInstallationToken ) . toHaveBeenCalledTimes ( 1 ) ;
42
132
expect ( generateFreshInstallationToken ) . toHaveBeenCalledTimes ( 0 ) ;
43
133
44
134
// after 10 minutes we expect a new token because the old one has expired
45
135
jest . setSystemTime ( in10Minutes ) ;
46
- const token3 = await installationTokenCache . getInstallationToken ( githubInstallationId , generateFreshInstallationToken ) ;
136
+ const token3 = await installationTokenCache . getInstallationToken ( githubInstallationId , undefined , generateFreshInstallationToken ) ;
47
137
expect ( token3 ) . toEqual ( freshInstallationToken ) ;
48
138
expect ( generateInitialInstallationToken ) . toHaveBeenCalledTimes ( 1 ) ;
49
139
expect ( generateFreshInstallationToken ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments