2
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
+ import exp from 'constants' ;
5
6
import { ModelDataStore , GenericData } from '../../lib/model-data' ;
6
- import { OAuthRelier } from './oauth-relier' ;
7
+ import { OAuthRelier , replaceItemInArray } from './oauth-relier' ;
7
8
8
9
describe ( 'models/reliers/oauth-relier' , function ( ) {
9
10
let data : ModelDataStore ;
@@ -25,5 +26,171 @@ describe('models/reliers/oauth-relier', function () {
25
26
expect ( model ) . toBeDefined ( ) ;
26
27
} ) ;
27
28
29
+ describe ( 'scope' , ( ) => {
30
+ const SCOPE = 'profile:email profile:uid' ;
31
+ const SCOPE_PROFILE = 'profile' ;
32
+ const SCOPE_PROFILE_UNRECOGNIZED = 'profile:unrecognized' ;
33
+ const SCOPE_WITH_PLUS = 'profile:email+profile:uid' ;
34
+ const SCOPE_WITH_EXTRAS =
35
+ 'profile:email profile:uid profile:non_whitelisted' ;
36
+ const SCOPE_WITH_OPENID = 'profile:email profile:uid openid' ;
37
+
38
+ function getRelierWithScope ( scope : string ) {
39
+ const relier = new OAuthRelier (
40
+ new GenericData ( {
41
+ scope,
42
+ } ) ,
43
+ new GenericData ( { } ) ,
44
+ {
45
+ scopedKeysEnabled : true ,
46
+ scopedKeysValidation : { } ,
47
+ isPromptNoneEnabled : true ,
48
+ isPromptNoneEnabledClientIds : [ ] ,
49
+ }
50
+ ) ;
51
+
52
+ relier . isTrusted = async ( ) => {
53
+ return true ;
54
+ } ;
55
+
56
+ return relier ;
57
+ }
58
+
59
+ describe ( 'is invalid' , ( ) => {
60
+ function getRelier ( scope : string ) {
61
+ return getRelierWithScope ( scope ) ;
62
+ }
63
+
64
+ it ( 'empty scope' , async ( ) => {
65
+ const relier = getRelier ( '' ) ;
66
+ await expect ( relier . getPermissions ( ) ) . rejects . toThrow ( ) ;
67
+ } ) ;
68
+
69
+ it ( 'whitespace scope' , async ( ) => {
70
+ const relier = getRelier ( ' ' ) ;
71
+ await expect ( relier . getPermissions ( ) ) . rejects . toThrow ( ) ;
72
+ } ) ;
73
+ } ) ;
74
+
75
+ describe ( 'is valid' , ( ) => {
76
+ function getRelier ( scope : string ) {
77
+ return getRelierWithScope ( scope ) ;
78
+ }
79
+
80
+ it ( `normalizes ${ SCOPE } ` , async ( ) => {
81
+ const relier = getRelier ( SCOPE ) ;
82
+ expect ( await relier . getNormalizedScope ( ) ) . toEqual (
83
+ 'profile:email profile:uid'
84
+ ) ;
85
+ } ) ;
86
+
87
+ it ( `transforms ${ SCOPE } to permissions` , async ( ) => {
88
+ const relier = getRelier ( SCOPE ) ;
89
+ expect ( await relier . getPermissions ( ) ) . toEqual ( [
90
+ 'profile:email' ,
91
+ 'profile:uid' ,
92
+ ] ) ;
93
+ } ) ;
94
+
95
+ it ( `transforms ${ SCOPE_WITH_PLUS } ` , async ( ) => {
96
+ const relier = getRelier ( SCOPE_WITH_PLUS ) ;
97
+ expect ( await relier . getPermissions ( ) ) . toEqual ( [
98
+ 'profile:email' ,
99
+ 'profile:uid' ,
100
+ ] ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ describe ( 'untrusted reliers' , ( ) => {
105
+ function getRelier ( scope : string ) {
106
+ const relier = getRelierWithScope ( scope ) ;
107
+ relier . isTrusted = async ( ) => {
108
+ return false ;
109
+ } ;
110
+ return relier ;
111
+ }
112
+
113
+ it ( `normalizes ${ SCOPE_WITH_EXTRAS } ` , async ( ) => {
114
+ const relier = getRelier ( SCOPE_WITH_EXTRAS ) ;
115
+ expect ( await relier . getNormalizedScope ( ) ) . toBe ( SCOPE ) ;
116
+ } ) ;
117
+
118
+ it ( `normalizes ${ SCOPE_WITH_OPENID } ` , async ( ) => {
119
+ const relier = getRelier ( SCOPE_WITH_OPENID ) ;
120
+ expect ( await relier . getNormalizedScope ( ) ) . toBe ( SCOPE_WITH_OPENID ) ;
121
+ } ) ;
122
+
123
+ it ( `prohibits ${ SCOPE_PROFILE } ` , async ( ) => {
124
+ const relier = getRelier ( SCOPE_PROFILE ) ;
125
+ await expect ( relier . getNormalizedScope ( ) ) . rejects . toThrow ( ) ;
126
+ } ) ;
127
+
128
+ it ( `prohibits ${ SCOPE_PROFILE_UNRECOGNIZED } ` , async ( ) => {
129
+ const relier = getRelier ( SCOPE_PROFILE_UNRECOGNIZED ) ;
130
+ await expect ( relier . getNormalizedScope ( ) ) . rejects . toThrow ( ) ;
131
+ } ) ;
132
+ } ) ;
133
+
134
+ describe ( 'trusted reliers that do not ask for consent' , ( ) => {
135
+ function getRelier ( scope : string ) {
136
+ const relier = getRelierWithScope ( scope ) ;
137
+ relier . wantsConsent = ( ) => {
138
+ return false ;
139
+ } ;
140
+ return relier ;
141
+ }
142
+
143
+ it ( `normalizes ${ SCOPE_WITH_EXTRAS } ` , async ( ) => {
144
+ const relier = getRelier ( SCOPE_WITH_EXTRAS ) ;
145
+ expect ( await relier . getNormalizedScope ( ) ) . toEqual ( SCOPE_WITH_EXTRAS ) ;
146
+ } ) ;
147
+
148
+ it ( `normalizes ${ SCOPE_PROFILE } ` , async ( ) => {
149
+ const relier = getRelier ( SCOPE_PROFILE ) ;
150
+ expect ( await relier . getNormalizedScope ( ) ) . toEqual ( SCOPE_PROFILE ) ;
151
+ } ) ;
152
+
153
+ it ( `normalizes ${ SCOPE_PROFILE_UNRECOGNIZED } ` , async ( ) => {
154
+ const relier = getRelier ( SCOPE_PROFILE_UNRECOGNIZED ) ;
155
+ expect ( await relier . getNormalizedScope ( ) ) . toEqual (
156
+ SCOPE_PROFILE_UNRECOGNIZED
157
+ ) ;
158
+ } ) ;
159
+ } ) ;
160
+ } ) ;
161
+
162
+ describe ( 'replaceItemInArray' , ( ) => {
163
+ it ( 'handles empty array' , ( ) => {
164
+ expect ( replaceItemInArray ( [ ] , 'foo' , [ 'bar' ] ) ) . toEqual ( [ ] ) ;
165
+ } ) ;
166
+
167
+ it ( 'handles miss' , ( ) => {
168
+ expect ( replaceItemInArray ( [ 'a' , 'b' , 'c' ] , '' , [ 'foo' ] ) ) . toEqual ( [
169
+ 'a' ,
170
+ 'b' ,
171
+ 'c' ,
172
+ ] ) ;
173
+ } ) ;
174
+
175
+ it ( 'replaces and preserves order' , ( ) => {
176
+ expect ( replaceItemInArray ( [ 'a' , 'b' , 'c' ] , 'b' , [ 'foo' , 'bar' ] ) ) . toEqual ( [
177
+ 'a' ,
178
+ 'foo' ,
179
+ 'bar' ,
180
+ 'c' ,
181
+ ] ) ;
182
+ } ) ;
183
+
184
+ it ( 'handles duplicates' , ( ) => {
185
+ expect (
186
+ replaceItemInArray ( [ 'a' , 'b' , 'b' , 'c' , 'c' ] , 'b' , [ 'foo' , 'foo' ] )
187
+ ) . toEqual ( [ 'a' , 'foo' , 'c' ] ) ;
188
+ } ) ;
189
+
190
+ it ( 'handles empty replacement' , ( ) => {
191
+ expect ( replaceItemInArray ( [ 'a' , 'b' , 'c' ] , 'a' , [ ] ) ) . toEqual ( [ 'b' , 'c' ] ) ;
192
+ } ) ;
193
+ } ) ;
194
+
28
195
// TODO: OAuth Relier Model Test Coverage
29
196
} ) ;
0 commit comments