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

Commit a1df4e4

Browse files
author
vikasrohit
committed
SUP-2481, intergrate-web-links-api
-- More unit tests -- Fixed removeLink method in externalLinks.service.js
1 parent 6c57b2c commit a1df4e4

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

app/services/externalAccounts.service.spec.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('ExternalAccount Service', function() {
77
var mockProfile = mockData.getMockProfile();
88
var apiUrl;
99
var auth0, userService;
10-
var profilePost, profileDelete;
10+
var profileGet, profilePost, profileDelete;
1111

1212

1313
beforeEach(function() {
@@ -46,9 +46,10 @@ describe('ExternalAccount Service', function() {
4646
$httpBackend
4747
.when('GET', apiUrl + '/members/test1/externalAccounts/')
4848
.respond(200, {result: {content: mockAccountsData}});
49-
$httpBackend
50-
.when('GET', apiUrl + '/users/111/?fields=profiles')
51-
.respond(200, {result: {content: mockUserLinksData}});
49+
50+
profileGet = $httpBackend.when('GET', apiUrl + '/users/111/?fields=profiles');
51+
profileGet.respond(200, {result: {content: mockUserLinksData}});
52+
5253
profilePost = $httpBackend.when('POST', apiUrl + '/users/111/profiles/');
5354
profilePost.respond(200, {result: {content: mockProfile}});
5455

@@ -104,6 +105,33 @@ describe('ExternalAccount Service', function() {
104105
$httpBackend.flush();
105106
});
106107

108+
it('should not return unsupported links even if they are returned by the API', function() {
109+
var profiles = JSON.parse(JSON.stringify(mockUserLinksData));
110+
profiles.profiles.push({providerType: 'unsupported'});
111+
profileGet.respond(200, {result: {content: profiles}});
112+
// spy
113+
service.getAllExternalLinks('test1', 111, true).then(function(data) {
114+
expect(data).to.be.defined;
115+
expect(_.pluck(data, 'provider')).to.include.members(['dribbble', 'github','bitbucket', 'stackoverflow']);
116+
expect(_.all(_.pluck(data, 'data'))).to.be.truthy;
117+
});
118+
$httpBackend.flush();
119+
});
120+
121+
it('should fail in returning links', function() {
122+
var errorMessage = "bad request";
123+
// mocks the GET call to respond with 400 bad request
124+
profileGet.respond(400, {result: { status: 400, content: errorMessage } });
125+
// calls getAllExternalLinks method with valid params
126+
service.getAllExternalLinks('test1', 111, true).then(function(data) {
127+
sinon.assert.fail('should not be called');
128+
}).catch(function(resp) {
129+
expect(resp).to.exist;
130+
expect(resp.status).to.exist.to.equal(400);
131+
});
132+
$httpBackend.flush();
133+
});
134+
107135
it('should link external account', function() {
108136
// call linkExternalAccount method with supporte network, should succeed
109137
service.linkExternalAccount('stackoverflow', "callback").then(function(data) {
@@ -151,6 +179,20 @@ describe('ExternalAccount Service', function() {
151179
$httpBackend.flush();
152180
});
153181

182+
it('should fail, with fatal error, in linking external account', function() {
183+
var errorMessage = "endpoint not found";
184+
profilePost.respond(404, {result: { status: 404, content: errorMessage } });
185+
// call unlinkExternalAccount method with supporte network, should succeed
186+
service.linkExternalAccount('stackoverflow', "callback").then(function(data) {
187+
sinon.assert.fail('should not be called');
188+
}).catch(function(error) {
189+
expect(error).to.be.defined;
190+
expect(error.status).to.exist.to.equal('FATAL_ERROR');
191+
expect(error.msg).to.exist.to.equal(errorMessage);
192+
});
193+
$httpBackend.flush();
194+
});
195+
154196
it('should unlink external account', function() {
155197
var errorMessage = "social profile exists";
156198
profilePost.respond(400, {result: { status: 400, content: errorMessage } });

app/services/externalLinks.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
}
5252

5353
function removeLink(userHandle, key) {
54-
return memberApi.one('members', userHandle).one('externalLinks', key).delete();
54+
return memberApi.one('members', userHandle).one('externalLinks', key).remove();
5555
}
5656

5757
}

app/services/externalLinks.service.spec.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@ describe('ExternalWebLinks service', function() {
33
var service;
44
var mockExternalLinks = mockData.getMockExternalWebLinksData();
55
var apiUrl;
6+
var linksGet, linksPost, linksDelete;
67

78

89
beforeEach(function() {
910
bard.appModule('topcoder');
10-
bard.inject(this, 'ExternalWebLinksService', '$httpBackend', 'CONSTANTS');
11+
bard.inject(this, 'ExternalWebLinksService', 'JwtInterceptorService', '$httpBackend', 'CONSTANTS', '$q');
12+
bard.mockService(JwtInterceptorService, {
13+
getToken: $q.when('token'),
14+
_default: $q.when([])
15+
});
1116

1217
apiUrl = CONSTANTS.API_URL;
1318
service = ExternalWebLinksService;
1419

1520
// mock profile api
16-
$httpBackend
17-
.when('GET', apiUrl + '/members/test1/externalLinks/')
18-
.respond(200, {result: {content: mockExternalLinks}});
21+
linksGet = $httpBackend.when('GET', apiUrl + '/members/test1/externalLinks/');
22+
linksGet.respond(200, {result: {content: mockExternalLinks}});
23+
24+
// mock profile api [POST]
25+
linksPost = $httpBackend.when('POST', apiUrl + '/members/test1/externalLinks/');
26+
linksPost.respond(200, {result: {content: mockExternalLinks[0]}});
27+
28+
// mock profile api [DELETE]
29+
linksDelete = $httpBackend.when('DELETE', apiUrl + '/members/test1/externalLinks/testkey/');
30+
linksDelete.respond(200, {result: {}});
1931
});
2032

2133
afterEach(function() {
@@ -41,4 +53,24 @@ describe('ExternalWebLinks service', function() {
4153
$httpBackend.flush();
4254
});
4355

56+
it('should add external link', function() {
57+
// call addLink method with valid params, should succeed
58+
service.addLink('test1', "http://google.com").then(function(newLink) {
59+
expect(newLink).to.be.exist;
60+
expect(newLink.provider).to.exist.to.equal('weblink');
61+
expect(newLink.data).to.exist;
62+
expect(newLink.data.status).to.exist.to.equal('pending');
63+
});
64+
$httpBackend.flush();
65+
});
66+
67+
it('should remove external link', function() {
68+
// call removeLink method with valid params, should succeed
69+
service.removeLink('test1', "testkey").then(function(newLink) {
70+
}).catch(function(error) {
71+
sinon.assert.fail('should not be called');
72+
});
73+
$httpBackend.flush();
74+
});
75+
4476
});

0 commit comments

Comments
 (0)