Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 13b4020

Browse files
author
vikasrohit
committed
SUP-2481, intergrate-web-links-api
-- Added more unit tests -- Fixed existing unit tests -- Fixed UI for Adding link
1 parent c254046 commit 13b4020

8 files changed

+156
-35
lines changed

app/directives/external-account/external-link-data.directive.jade

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
.title {{account.data.title}}
100100

101101
div(ng-switch-when="weblink")
102-
.handle My Portfoilo
102+
.handle {{account.title}}
103103

104-
.title {{account.data.title}}
104+
.title {{account.description}}
105+
106+
.title {{account.URL}}

app/directives/external-account/external-web-link.directive.jade

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
p(ng-show="addWebLinkFrm.url.$error.url") Please enter a valid URL
1010

11-
button.tc-btn.tc-btn-secondary.tc-btn-s(type="submit",
11+
button.tc-btn.tc-btn-m(type="submit",
1212
tc-busy-button, tc-busy-when="vm.addingWebLink", tc-busy-message="Adding",
13-
ng-disabled="addWebLinkFrm.$invalid || addWebLinkFrm.$pristine",
14-
ng-class="{'enabled-button': addWebLinkFrm.$valid, 'disabled': addWebLinkFrm.$pristine || addWebLinkFrm.$invalid, 'busy' : vm.addingWebLink}") Add
13+
ng-disabled="addWebLinkFrm.$invalid || addWebLinkFrm.$pristine") Add
1514

1615
.form-errors(ng-show="vm.errorMessage")
1716
p.form-error {{vm.errorMessage}}

app/directives/external-account/external-web-links.directive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
angular
99
.module('tcUIComponents')
1010
.directive('externalWebLink', ExternalWebLink);
11-
ExternalWebLink.$inject = ['$log', 'ExternalWebLinksService'];
11+
ExternalWebLink.$inject = ['$log', 'ExternalWebLinksService', 'toaster'];
1212

13-
function ExternalWebLink($log, ExternalWebLinksService) {
13+
function ExternalWebLink($log, ExternalWebLinksService, toaster) {
1414
var directive = {
1515
estrict: 'E',
1616
templateUrl: 'directives/external-account/external-web-link.directive.html',

app/services/externalAccounts.service.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
.withHttpConfig({skipAuthorization: true})
4545
.customGET('externalAccounts')
4646
.then(function(data) {
47-
// TODO workaround for dribbble spelling mistake, remove once API is fixed
48-
if (data.dribble) {
49-
data.dribbble = data.dribble;
50-
}
5147
return data;
5248
});
5349
}
@@ -166,7 +162,7 @@
166162
}
167163
};
168164
_data.linkedAccount.data.status = 'PENDING';
169-
return _data;
165+
resolve(_data);
170166
})
171167
.catch(function(resp) {
172168
var errorStatus = "FATAL_ERROR";

app/services/externalAccounts.service.spec.js

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,57 @@ describe('ExternalAccount Service', function() {
33
var service;
44
var mockAccountsData = mockData.getMockLinkedExternalAccountsData();
55
var mockUserLinksData = mockData.getMockLinkedExternalAccounts();
6+
var mockAuth0Profile = mockData.getMockAuth0Profile();
7+
var mockProfile = mockData.getMockProfile();
68
var apiUrl;
9+
var auth0, userService;
10+
var profilePost, profileDelete;
711

812

913
beforeEach(function() {
1014
bard.appModule('topcoder');
11-
bard.inject(this, 'ExternalAccountService', '$httpBackend', '$q', 'CONSTANTS', 'JwtInterceptorService');
15+
bard.inject(this, 'ExternalAccountService', '$httpBackend', '$q', 'CONSTANTS', 'JwtInterceptorService', 'auth', 'UserService');
1216
bard.mockService(JwtInterceptorService, {
1317
getToken: $q.when('token'),
1418
_default: $q.when([])
1519
});
1620

1721
apiUrl = CONSTANTS.API_URL;
1822
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+
});
1945

2046
$httpBackend
2147
.when('GET', apiUrl + '/members/test1/externalAccounts/')
2248
.respond(200, {result: {content: mockAccountsData}});
2349
$httpBackend
2450
.when('GET', apiUrl + '/users/111/?fields=profiles')
2551
.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}});
2657

2758
});
2859

@@ -73,4 +104,91 @@ describe('ExternalAccount Service', function() {
73104
$httpBackend.flush();
74105
});
75106

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+
76194
});

app/services/externalLinks.service.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
function addLink(userHandle, url) {
4242
return memberApi.one('members', userHandle).customPOST({'url': url}, 'externalLinks')
4343
.then(function(resp) {
44-
debugger;
4544
var _newLink = {
4645
provider: 'weblink',
4746
data: resp

assets/css/directives/external-web-link.scss

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
@import 'topcoder-includes';
2-
@import '../partials/combined';
3-
1+
@import 'tc-includes';
42

53
external-web-link {
64
.web-link {
@@ -33,9 +31,10 @@ external-web-link {
3331
.form-field-focused {
3432
@include form-field-focused;
3533
}
36-
34+
3735
.validation-bar.url {
3836
flex: 1;
37+
width: auto;
3938

4039
&:before {
4140
height: 40px;
@@ -47,23 +46,10 @@ external-web-link {
4746
margin: 0px;
4847
}
4948

50-
// button {
51-
// @include button-2-m;
52-
// font-size: 14px;
53-
// line-height: 16px;
54-
// height: 40px;
55-
// width: 60px;
56-
// margin: 0 0 0 10px;
57-
// text-transform: uppercase;
58-
59-
// &.enabled-button {
60-
// @include ui-enabled-button;
61-
// }
62-
63-
// &.busy {
64-
// width: 94px;
65-
// }
66-
// }
49+
button {
50+
width: auto;
51+
margin-left: 10px;
52+
}
6753
}
6854
}
6955
}

tests/test-helpers/mock-data.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var mockData = (function() {
2424
getMockLinkedExternalAccounts: getMockLinkedExternalAccounts,
2525
getMockLinkedExternalAccountsData: getMockLinkedExternalAccountsData,
2626
getMockExternalWebLinksData: getMockExternalWebLinksData,
27+
getMockAuth0Profile: getMockAuth0Profile
2728
};
2829

2930
function getMockStates() {
@@ -2054,4 +2055,24 @@ var mockData = (function() {
20542055
]
20552056
}
20562057

2058+
function getMockAuth0Profile() {
2059+
return {
2060+
"user_id": "mockSocialNetwork|123456",
2061+
"given_name": "mock",
2062+
"family_name": "user",
2063+
"first_name": "mock",
2064+
"last_name": "user",
2065+
"nickname": "mocky",
2066+
"name": "mock user",
2067+
"email": "[email protected]",
2068+
"username": "mockuser",
2069+
"identities": [
2070+
{
2071+
"access_token": "abcdefghi",
2072+
"access_token_secret": "abcdefghijklmnopqrstuvwxyz"
2073+
}
2074+
]
2075+
};
2076+
}
2077+
20572078
})();

0 commit comments

Comments
 (0)