1
- import {
2
- getPageTitles ,
3
- createQueryParams ,
4
- createNestedQueryParams ,
5
- getCsrfToken ,
6
- } from "../utils" ;
7
- import cookieUtils from "../CookieUtils" ;
8
-
9
- jest . mock ( "../CookieUtils" ) ;
1
+ import * as utils from "../utils" ;
10
2
11
3
describe ( "getPageTiles" , ( ) => {
12
4
test ( "it returns text saying there are 5 steps" , ( ) => {
13
- expect ( getPageTitles ( ) ) . toEqual ( {
5
+ expect ( utils . getPageTitles ( ) ) . toEqual ( {
14
6
personal : "Step 1 of 5: Personal Information" ,
15
7
address : "Step 2 of 5: Address" ,
16
8
workAddress : "Alternate Address" ,
@@ -23,7 +15,7 @@ describe("getPageTiles", () => {
23
15
24
16
describe ( "createQueryParams" , ( ) => {
25
17
test ( "it should return an empty string with an empty object" , ( ) => {
26
- expect ( createQueryParams ( { } ) ) . toEqual ( "" ) ;
18
+ expect ( utils . createQueryParams ( { } ) ) . toEqual ( "" ) ;
27
19
} ) ;
28
20
29
21
test ( "it should return a url query string" , ( ) => {
@@ -32,16 +24,16 @@ describe("createQueryParams", () => {
32
24
key2 : "value2" ,
33
25
key3 : "value3" ,
34
26
} ;
35
- expect ( createQueryParams ( data ) ) . toEqual (
27
+ expect ( utils . createQueryParams ( data ) ) . toEqual (
36
28
"&key1=value1&key2=value2&key3=value3"
37
29
) ;
38
30
} ) ;
39
31
} ) ;
40
32
41
33
describe ( "createNestedQueryParams" , ( ) => {
42
34
test ( "it should return an empty string with an empty string or type argument" , ( ) => {
43
- expect ( createNestedQueryParams ( { } , "key" ) ) . toEqual ( "" ) ;
44
- expect ( createNestedQueryParams ( { key : "somevalue" } , "" ) ) . toEqual ( "" ) ;
35
+ expect ( utils . createNestedQueryParams ( { } , "key" ) ) . toEqual ( "" ) ;
36
+ expect ( utils . createNestedQueryParams ( { key : "somevalue" } , "" ) ) . toEqual ( "" ) ;
45
37
} ) ;
46
38
47
39
test ( "it should return a nested url query string" , ( ) => {
@@ -50,46 +42,12 @@ describe("createNestedQueryParams", () => {
50
42
key2 : "value2" ,
51
43
key3 : "value3" ,
52
44
} ;
53
- expect ( createNestedQueryParams ( data , "results" ) ) . toEqual (
45
+ expect ( utils . createNestedQueryParams ( data , "results" ) ) . toEqual (
54
46
`&results=${ JSON . stringify ( data ) } `
55
47
) ;
56
48
57
- expect ( createNestedQueryParams ( data , "errors" ) ) . toEqual (
49
+ expect ( utils . createNestedQueryParams ( data , "errors" ) ) . toEqual (
58
50
`&errors=${ JSON . stringify ( data ) } `
59
51
) ;
60
52
} ) ;
61
53
} ) ;
62
-
63
- describe ( "getCsrfToken" , ( ) => {
64
- beforeAll ( ( ) => {
65
- // We don't actually want to set any cookies so mock this.
66
- cookieUtils . set = jest . fn ( ( ) => "ok" ) ;
67
- } ) ;
68
-
69
- test ( "it returns a new token which is not valid since it's new and not compared to an existing token" , ( ) => {
70
- const { csrfToken, csrfTokenValid } = getCsrfToken ( { cookies : { } } , { } ) ;
71
- // We don't really care what it is, just that it's there.
72
- expect ( csrfToken ) . toBeDefined ( ) ;
73
- expect ( csrfTokenValid ) . toEqual ( false ) ;
74
- } ) ;
75
-
76
- // TODO: it's hard to test when the true case happens because the secret is
77
- // private in the function, by design and security.
78
- test ( "it returns a false token validation" , ( ) => {
79
- const firstCall = getCsrfToken ( { cookies : { } } , { } ) ;
80
-
81
- expect ( firstCall . csrfToken ) . toBeDefined ( ) ;
82
- expect ( firstCall . csrfTokenValid ) . toEqual ( false ) ;
83
-
84
- const second = getCsrfToken (
85
- {
86
- cookies : { "next-auth.csrf-token" : "wrong-token!" } ,
87
- } ,
88
- { }
89
- ) ;
90
-
91
- // The first token should not be reused.
92
- expect ( second . csrfToken === firstCall . csrfToken ) . toEqual ( false ) ;
93
- expect ( second . csrfTokenValid ) . toEqual ( false ) ;
94
- } ) ;
95
- } ) ;
0 commit comments