2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ import 'dart:convert' ;
6
+
5
7
import 'package:flutter_test/flutter_test.dart' ;
6
8
import 'package:google_identity_services_web/id.dart' ;
7
9
import 'package:google_identity_services_web/oauth2.dart' ;
@@ -18,6 +20,7 @@ void main() {
18
20
group ('gisResponsesToTokenData' , () {
19
21
testWidgets ('null objects -> no problem' , (_) async {
20
22
final GoogleSignInTokenData tokens = gisResponsesToTokenData (null , null );
23
+
21
24
expect (tokens.accessToken, isNull);
22
25
expect (tokens.idToken, isNull);
23
26
expect (tokens.serverAuthCode, isNull);
@@ -36,6 +39,7 @@ void main() {
36
39
});
37
40
final GoogleSignInTokenData tokens =
38
41
gisResponsesToTokenData (credential, token);
42
+
39
43
expect (tokens.accessToken, expectedAccessToken);
40
44
expect (tokens.idToken, expectedIdToken);
41
45
expect (tokens.serverAuthCode, isNull);
@@ -44,22 +48,21 @@ void main() {
44
48
45
49
group ('gisResponsesToUserData' , () {
46
50
testWidgets ('happy case' , (_) async {
47
- final GoogleSignInUserData data = gisResponsesToUserData (okCredential )! ;
51
+ final GoogleSignInUserData data = gisResponsesToUserData (goodCredential )! ;
48
52
49
53
expect (data.displayName, 'Vincent Adultman' );
50
54
expect (data.id, '123456' );
51
55
expect (data.email,
'[email protected] ' );
52
56
expect (data.photoUrl, 'https://thispersondoesnotexist.com/image?x=.jpg' );
53
- expect (data.idToken, okCredential.credential );
57
+ expect (data.idToken, goodJwtToken );
54
58
});
55
59
56
60
testWidgets ('null response -> null' , (_) async {
57
61
expect (gisResponsesToUserData (null ), isNull);
58
62
});
59
63
60
64
testWidgets ('null response.credential -> null' , (_) async {
61
- final CredentialResponse response = nullCredential;
62
- expect (gisResponsesToUserData (response), isNull);
65
+ expect (gisResponsesToUserData (nullCredential), isNull);
63
66
});
64
67
65
68
testWidgets ('invalid payload -> null' , (_) async {
@@ -70,4 +73,101 @@ void main() {
70
73
expect (gisResponsesToUserData (response), isNull);
71
74
});
72
75
});
76
+
77
+ group ('getJwtTokenPayload' , () {
78
+ testWidgets ('happy case -> data' , (_) async {
79
+ final Map <String , Object ?>? data = getJwtTokenPayload (goodJwtToken);
80
+
81
+ expect (data, isNotNull);
82
+ expect (data, containsPair ('name' , 'Vincent Adultman' ));
83
+ expect (data,
containsPair (
'email' ,
'[email protected] ' ));
84
+ expect (data, containsPair ('sub' , '123456' ));
85
+ expect (
86
+ data,
87
+ containsPair (
88
+ 'picture' ,
89
+ 'https://thispersondoesnotexist.com/image?x=.jpg' ,
90
+ ));
91
+ });
92
+
93
+ testWidgets ('null Token -> null' , (_) async {
94
+ final Map <String , Object ?>? data = getJwtTokenPayload (null );
95
+
96
+ expect (data, isNull);
97
+ });
98
+
99
+ testWidgets ('Token not matching the format -> null' , (_) async {
100
+ final Map <String , Object ?>? data = getJwtTokenPayload ('1234.4321' );
101
+
102
+ expect (data, isNull);
103
+ });
104
+
105
+ testWidgets ('Bad token that matches the format -> null' , (_) async {
106
+ final Map <String , Object ?>? data = getJwtTokenPayload ('1234.abcd.4321' );
107
+
108
+ expect (data, isNull);
109
+ });
110
+ });
111
+
112
+ group ('decodeJwtPayload' , () {
113
+ testWidgets ('Good payload -> data' , (_) async {
114
+ final Map <String , Object ?>? data = decodeJwtPayload (goodPayload);
115
+
116
+ expect (data, isNotNull);
117
+ expect (data, containsPair ('name' , 'Vincent Adultman' ));
118
+ expect (data,
containsPair (
'email' ,
'[email protected] ' ));
119
+ expect (data, containsPair ('sub' , '123456' ));
120
+ expect (
121
+ data,
122
+ containsPair (
123
+ 'picture' ,
124
+ 'https://thispersondoesnotexist.com/image?x=.jpg' ,
125
+ ));
126
+ });
127
+
128
+ testWidgets ('Proper JSON payload -> data' , (_) async {
129
+ final String payload = base64.encode (utf8.encode ('{"properJson": true}' ));
130
+
131
+ final Map <String , Object ?>? data = decodeJwtPayload (payload);
132
+
133
+ expect (data, isNotNull);
134
+ expect (data, containsPair ('properJson' , true ));
135
+ });
136
+
137
+ testWidgets ('Not-normalized base-64 payload -> data' , (_) async {
138
+ // This is the payload generated by the "Proper JSON payload" test, but
139
+ // we remove the leading "=" symbols so it's length is not a multiple of 4
140
+ // anymore!
141
+ final String payload = 'eyJwcm9wZXJKc29uIjogdHJ1ZX0=' .replaceAll ('=' , '' );
142
+
143
+ final Map <String , Object ?>? data = decodeJwtPayload (payload);
144
+
145
+ expect (data, isNotNull);
146
+ expect (data, containsPair ('properJson' , true ));
147
+ });
148
+
149
+ testWidgets ('Invalid JSON payload -> null' , (_) async {
150
+ final String payload = base64.encode (utf8.encode ('{properJson: false}' ));
151
+
152
+ final Map <String , Object ?>? data = decodeJwtPayload (payload);
153
+
154
+ expect (data, isNull);
155
+ });
156
+
157
+ testWidgets ('Non JSON payload -> null' , (_) async {
158
+ final String payload = base64.encode (utf8.encode ('not-json' ));
159
+
160
+ final Map <String , Object ?>? data = decodeJwtPayload (payload);
161
+
162
+ expect (data, isNull);
163
+ });
164
+
165
+ testWidgets ('Non base-64 payload -> null' , (_) async {
166
+ const String payload = 'not-base-64-at-all' ;
167
+
168
+ final Map <String , Object ?>? data = decodeJwtPayload (payload);
169
+
170
+ expect (data, isNull);
171
+ });
172
+ });
73
173
}
0 commit comments