@@ -17,6 +17,7 @@ import crypto from 'crypto';
1717import { promises as fs } from 'fs' ;
1818import { FSCachePlugin } from '../src/index.js' ;
1919import { MockTokenCacheContext } from './MockTokenCacheContext.js' ;
20+ import { toAuthContent } from './utils.js' ;
2021
2122describe ( 'FSCachePlugin Test' , ( ) => {
2223 let testRoot ;
@@ -37,21 +38,20 @@ describe('FSCachePlugin Test', () => {
3738 filePath : testFilePath ,
3839 } ) ;
3940
41+ const data = toAuthContent ( '1234' ) ;
4042 const ctx = new MockTokenCacheContext ( {
4143 cacheHasChanged : true ,
42- tokens : '{ "access_token": "1234" }' ,
44+ data ,
4345 } ) ;
4446 const ret = await p . afterCacheAccess ( ctx ) ;
4547 assert . strictEqual ( ret , true ) ;
46- assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , {
47- access_token : '1234' ,
48- } ) ;
48+ assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , data ) ;
4949 assert . strictEqual ( p . location , testFilePath ) ;
5050 } ) ;
5151
5252 it ( 'writes the fresh cache data does not overwrite metadata' , async ( ) => {
5353 await fs . writeFile ( testFilePath , JSON . stringify ( {
54- access_token : '1234' ,
54+ ... toAuthContent ( '1234' ) ,
5555 cachePluginMetadata : {
5656 foo : 'bar' ,
5757 } ,
@@ -61,14 +61,15 @@ describe('FSCachePlugin Test', () => {
6161 filePath : testFilePath ,
6262 } ) ;
6363
64+ const data = toAuthContent ( '2345' ) ;
6465 const ctx = new MockTokenCacheContext ( {
6566 cacheHasChanged : true ,
66- tokens : '{ "access_token": "1234" }' ,
67+ data ,
6768 } ) ;
6869 const ret = await p . afterCacheAccess ( ctx ) ;
6970 assert . strictEqual ( ret , true ) ;
7071 assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , {
71- access_token : '1234' ,
72+ ... data ,
7273 cachePluginMetadata : {
7374 foo : 'bar' ,
7475 } ,
@@ -81,19 +82,18 @@ describe('FSCachePlugin Test', () => {
8182 filePath : testFilePath ,
8283 } ) ;
8384
85+ const data = toAuthContent ( '2345' ) ;
8486 const ctx = new MockTokenCacheContext ( {
8587 cacheHasChanged : true ,
86- tokens : '{ "access_token": "1234" }' ,
88+ data ,
8789 } ) ;
8890 const ret = await p . afterCacheAccess ( ctx ) ;
8991 assert . strictEqual ( ret , true ) ;
90- assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , {
91- access_token : '1234' ,
92- } ) ;
92+ assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , data ) ;
9393
9494 await p . setPluginMetadata ( { foo : 'bar' } ) ;
9595 assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , {
96- access_token : '1234' ,
96+ ... data ,
9797 cachePluginMetadata : {
9898 foo : 'bar' ,
9999 } ,
@@ -115,8 +115,9 @@ describe('FSCachePlugin Test', () => {
115115 } ) ;
116116
117117 it ( 'can clear plugin metadata' , async ( ) => {
118+ const data = toAuthContent ( '2345' ) ;
118119 await fs . writeFile ( testFilePath , JSON . stringify ( {
119- access_token : '1234' ,
120+ ... data ,
120121 cachePluginMetadata : {
121122 foo : 'bar' ,
122123 } ,
@@ -126,9 +127,7 @@ describe('FSCachePlugin Test', () => {
126127 filePath : testFilePath ,
127128 } ) ;
128129 await p . setPluginMetadata ( ) ;
129- assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , {
130- access_token : '1234' ,
131- } ) ;
130+ assert . deepStrictEqual ( JSON . parse ( await fs . readFile ( testFilePath , 'utf-8' ) ) , data ) ;
132131 } ) ;
133132
134133 it ( 'does not the cache data to the filesystem if context not changed' , async ( ) => {
@@ -138,7 +137,7 @@ describe('FSCachePlugin Test', () => {
138137
139138 const ctx = new MockTokenCacheContext ( {
140139 cacheHasChanged : false ,
141- tokens : '{ "access_token": "1234" }' ,
140+ data : toAuthContent ( '2345' ) ,
142141 } ) ;
143142 const ret = await p . afterCacheAccess ( ctx ) ;
144143 assert . strictEqual ( ret , false ) ;
@@ -153,7 +152,8 @@ describe('FSCachePlugin Test', () => {
153152 } ) ;
154153
155154 it ( 'read cache data from the filesystem' , async ( ) => {
156- await fs . writeFile ( testFilePath , '{ "access_token": "1234" }' , 'utf-8' ) ;
155+ const data = toAuthContent ( '1234' ) ;
156+ await fs . writeFile ( testFilePath , JSON . stringify ( data ) , 'utf-8' ) ;
157157
158158 const p = new FSCachePlugin ( {
159159 filePath : testFilePath ,
@@ -163,12 +163,12 @@ describe('FSCachePlugin Test', () => {
163163 } ) ;
164164 const ret = await p . beforeCacheAccess ( ctx ) ;
165165 assert . strictEqual ( ret , true ) ;
166- assert . strictEqual ( ctx . tokens , '{ "access_token": " 1234" } ' ) ;
166+ assert . strictEqual ( ctx . token , '1234' ) ;
167167 } ) ;
168168
169169 it ( 'read cache plugin metadata from the filesystem' , async ( ) => {
170170 await fs . writeFile ( testFilePath , JSON . stringify ( {
171- access_token : '1234' ,
171+ ... toAuthContent ( '1234' ) ,
172172 cachePluginMetadata : {
173173 foo : 'bar' ,
174174 } ,
@@ -182,15 +182,15 @@ describe('FSCachePlugin Test', () => {
182182 } ) ;
183183 const ret = await p . beforeCacheAccess ( ctx ) ;
184184 assert . strictEqual ( ret , true ) ;
185- assert . strictEqual ( ctx . tokens , '{"access_token":" 1234"} ' ) ;
185+ assert . strictEqual ( ctx . token , '1234' ) ;
186186 assert . deepStrictEqual ( await p . getPluginMetadata ( ) , {
187187 foo : 'bar' ,
188188 } ) ;
189189 } ) ;
190190
191191 it ( 'read cache plugin metadata from pristine plugin' , async ( ) => {
192192 await fs . writeFile ( testFilePath , JSON . stringify ( {
193- access_token : '1234' ,
193+ ... toAuthContent ( '1234' ) ,
194194 cachePluginMetadata : {
195195 foo : 'bar' ,
196196 } ,
@@ -213,23 +213,11 @@ describe('FSCachePlugin Test', () => {
213213 } ) ;
214214 const ret = await p . beforeCacheAccess ( ctx ) ;
215215 assert . strictEqual ( ret , false ) ;
216- assert . strictEqual ( ctx . tokens , '' ) ;
217- } ) ;
218-
219- it ( 'read cache data warns about other errors' , async ( ) => {
220- const p = new FSCachePlugin ( {
221- filePath : testFilePath ,
222- } ) ;
223-
224- const ctx = new MockTokenCacheContext ( {
225- } ) ;
226- const ret = await p . beforeCacheAccess ( ctx ) ;
227- assert . strictEqual ( ret , false ) ;
228- assert . strictEqual ( ctx . tokens , '' ) ;
216+ assert . strictEqual ( ctx . token , '' ) ;
229217 } ) ;
230218
231219 it ( 'deletes the cache from the filesystem' , async ( ) => {
232- await fs . writeFile ( testFilePath , '{ "access_token": " 1234" }' , 'utf-8' ) ;
220+ await fs . writeFile ( testFilePath , JSON . stringify ( toAuthContent ( ' 1234' ) ) , 'utf-8' ) ;
233221 const p = new FSCachePlugin ( {
234222 filePath : testFilePath ,
235223 } ) ;
0 commit comments