@@ -3,26 +3,57 @@ describe('ExternalAccount Service', function() {
3
3
var service ;
4
4
var mockAccountsData = mockData . getMockLinkedExternalAccountsData ( ) ;
5
5
var mockUserLinksData = mockData . getMockLinkedExternalAccounts ( ) ;
6
+ var mockAuth0Profile = mockData . getMockAuth0Profile ( ) ;
7
+ var mockProfile = mockData . getMockProfile ( ) ;
6
8
var apiUrl ;
9
+ var auth0 , userService ;
10
+ var profilePost , profileDelete ;
7
11
8
12
9
13
beforeEach ( function ( ) {
10
14
bard . appModule ( 'topcoder' ) ;
11
- bard . inject ( this , 'ExternalAccountService' , '$httpBackend' , '$q' , 'CONSTANTS' , 'JwtInterceptorService' ) ;
15
+ bard . inject ( this , 'ExternalAccountService' , '$httpBackend' , '$q' , 'CONSTANTS' , 'JwtInterceptorService' , 'auth' , 'UserService' ) ;
12
16
bard . mockService ( JwtInterceptorService , {
13
17
getToken : $q . when ( 'token' ) ,
14
18
_default : $q . when ( [ ] )
15
19
} ) ;
16
20
17
21
apiUrl = CONSTANTS . API_URL ;
18
22
service = ExternalAccountService ;
23
+ auth0 = auth ;
24
+ userService = UserService ;
25
+
26
+ // mock user api
27
+ sinon . stub ( auth0 , 'signin' , function ( params , successCallback , failureCallback ) {
28
+ if ( params && params . state == 'failure' ) {
29
+ failureCallback . call ( failureCallback , "MOCK_ERROR" ) ;
30
+ }
31
+ successCallback . call (
32
+ successCallback ,
33
+ mockAuth0Profile ,
34
+ "mockAuth0IdToken" ,
35
+ "mockAuth0AccessToken" ,
36
+ params . state ,
37
+ null
38
+ ) ;
39
+ } ) ;
40
+
41
+ // mock user service
42
+ sinon . stub ( userService , 'getUserIdentity' , function ( ) {
43
+ return { userId : 111 , handle : mockProfile . handle } ;
44
+ } ) ;
19
45
20
46
$httpBackend
21
47
. when ( 'GET' , apiUrl + '/members/test1/externalAccounts/' )
22
48
. respond ( 200 , { result : { content : mockAccountsData } } ) ;
23
49
$httpBackend
24
50
. when ( 'GET' , apiUrl + '/users/111/?fields=profiles' )
25
51
. respond ( 200 , { result : { content : mockUserLinksData } } ) ;
52
+ profilePost = $httpBackend . when ( 'POST' , apiUrl + '/users/111/profiles/' ) ;
53
+ profilePost . respond ( 200 , { result : { content : mockProfile } } ) ;
54
+
55
+ profileDelete = $httpBackend . when ( 'DELETE' , apiUrl + '/users/111/profiles/stackoverflow/' ) ;
56
+ profileDelete . respond ( 200 , { result : { content : mockProfile } } ) ;
26
57
27
58
} ) ;
28
59
@@ -73,4 +104,91 @@ describe('ExternalAccount Service', function() {
73
104
$httpBackend . flush ( ) ;
74
105
} ) ;
75
106
107
+ it ( 'should link external account' , function ( ) {
108
+ // call linkExternalAccount method with supporte network, should succeed
109
+ service . linkExternalAccount ( 'stackoverflow' , "callback" ) . then ( function ( data ) {
110
+ expect ( data ) . to . be . defined ;
111
+ // console.log(data);
112
+ expect ( data . status ) . to . exist . to . equal ( 'SUCCESS' ) ;
113
+ expect ( data . linkedAccount ) . to . exist ;
114
+ expect ( data . linkedAccount . provider ) . to . exist . to . equal ( 'stackoverflow' ) ;
115
+ expect ( data . linkedAccount . data ) . to . exist ;
116
+ expect ( data . linkedAccount . data . status ) . to . exist . to . equal ( 'PENDING' ) ;
117
+ } ) ;
118
+ $httpBackend . flush ( ) ;
119
+ } ) ;
120
+
121
+ it ( 'should fail with unsupported network' , function ( ) {
122
+ // call linkExternalAccount method with unsupported network, should fail
123
+ service . linkExternalAccount ( 'unsupported' , "callback" ) . then ( function ( data ) {
124
+ expect ( data ) . to . be . defined ;
125
+ expect ( data . status ) . to . exist . to . equal ( 'failed' ) ;
126
+ expect ( data . error . to . contain ( 'unsupported' ) ) ;
127
+ } ) ;
128
+ } ) ;
129
+
130
+ it ( 'should fail with already existing profile' , function ( ) {
131
+ var errorMessage = "social profile exists" ;
132
+ profilePost . respond ( 400 , { result : { status : 400 , content : errorMessage } } ) ;
133
+ // call linkExternalAccount method, having user service throw already exist
134
+ service . linkExternalAccount ( 'stackoverflow' , "callback" ) . then ( function ( data ) {
135
+ sinon . assert . fail ( 'should not be called' ) ;
136
+ } , function ( error ) {
137
+ expect ( error ) . to . be . defined ;
138
+ expect ( error . status ) . to . exist . to . equal ( 'SOCIAL_PROFILE_ALREADY_EXISTS' ) ;
139
+ expect ( error . msg ) . to . exist . to . equal ( errorMessage ) ;
140
+ } ) ;
141
+ $httpBackend . flush ( ) ;
142
+ } ) ;
143
+
144
+ it ( 'should fail with auth0 error' , function ( ) {
145
+ // call linkExternalAccount method with auth0 throwing error
146
+ service . linkExternalAccount ( 'stackoverflow' , "failure" ) . then ( function ( data ) {
147
+ sinon . assert . fail ( 'should not be called' ) ;
148
+ } , function ( error ) {
149
+ expect ( error ) . to . be . exist . to . equal ( 'MOCK_ERROR' ) ;
150
+ } ) ;
151
+ $httpBackend . flush ( ) ;
152
+ } ) ;
153
+
154
+ it ( 'should unlink external account' , function ( ) {
155
+ var errorMessage = "social profile exists" ;
156
+ profilePost . respond ( 400 , { result : { status : 400 , content : errorMessage } } ) ;
157
+ // call unlinkExternalAccount method with supporte network, should succeed
158
+ service . unlinkExternalAccount ( 'stackoverflow' ) . then ( function ( data ) {
159
+ expect ( data ) . to . be . defined ;
160
+ // console.log(data);
161
+ expect ( data . status ) . to . exist . to . equal ( 'SUCCESS' ) ;
162
+ } ) ;
163
+ $httpBackend . flush ( ) ;
164
+ } ) ;
165
+
166
+ it ( 'should fail, with profile does not exist, in unlinking external account' , function ( ) {
167
+ var errorMessage = "social profile does not exists" ;
168
+ profileDelete . respond ( 404 , { result : { status : 404 , content : errorMessage } } ) ;
169
+ // call unlinkExternalAccount method with supporte network, should succeed
170
+ service . unlinkExternalAccount ( 'stackoverflow' ) . then ( function ( data ) {
171
+ sinon . assert . fail ( 'should not be called' ) ;
172
+ } ) . catch ( function ( error ) {
173
+ expect ( error ) . to . be . defined ;
174
+ expect ( error . status ) . to . exist . to . equal ( 'SOCIAL_PROFILE_NOT_EXIST' ) ;
175
+ expect ( error . msg ) . to . exist . to . equal ( errorMessage ) ;
176
+ } ) ;
177
+ $httpBackend . flush ( ) ;
178
+ } ) ;
179
+
180
+ it ( 'should fail, with fatal error, in unlinking external account' , function ( ) {
181
+ var errorMessage = "bad request" ;
182
+ profileDelete . respond ( 400 , { result : { status : 400 , content : errorMessage } } ) ;
183
+ // call unlinkExternalAccount method with supporte network, should succeed
184
+ service . unlinkExternalAccount ( 'stackoverflow' ) . then ( function ( data ) {
185
+ sinon . assert . fail ( 'should not be called' ) ;
186
+ } ) . catch ( function ( error ) {
187
+ expect ( error ) . to . be . defined ;
188
+ expect ( error . status ) . to . exist . to . equal ( 'FATAL_ERROR' ) ;
189
+ expect ( error . msg ) . to . exist . to . equal ( errorMessage ) ;
190
+ } ) ;
191
+ $httpBackend . flush ( ) ;
192
+ } ) ;
193
+
76
194
} ) ;
0 commit comments