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

Commit 36eeffa

Browse files
author
vikasrohit
committed
Unit tests for getSocialUserData and getPageTitle methods
1 parent a285984 commit 36eeffa

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

app/services/helpers.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
});
235235
// now loop over all keys and replace with compiled value
236236
Object.keys(compiledMap).forEach(function(k) {
237-
template = template.replace(k, compiledMap[k])
237+
template = template.replace(k, (compiledMap[k] ? compiledMap[k] : ''));
238238
});
239239
}
240240
return template;

app/services/helpers.service.spec.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,184 @@ describe('Helper Service', function() {
5454
expect($location.url).to.have.been.calledWith("/members/test1/");
5555
});
5656
});
57+
58+
describe("getSocialUserData()", function() {
59+
var mockProfile;
60+
beforeEach(function() {
61+
mockProfile = mockData.getMockAuth0Profile();
62+
});
63+
it("should get JSON for facebook user data ", function() {
64+
mockProfile.identities[0].connection = 'facebook';
65+
var socialData = Helpers.getSocialUserData(mockProfile, "");
66+
expect(socialData).to.exist.not.null;
67+
expect(socialData.socialUserId).to.exist.to.equal('123456');
68+
// TODO cross check population of username for all networks
69+
expect(socialData.username).to.exist.to.equal(mockProfile.first_name + '.' + mockProfile.last_name);
70+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
71+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
72+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
73+
expect(socialData.socialProvider).to.exist.to.equal('facebook');
74+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
75+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
76+
});
77+
78+
it("should get JSON for github user data ", function() {
79+
mockProfile.identities[0].connection = 'github';
80+
var socialData = Helpers.getSocialUserData(mockProfile, "");
81+
expect(socialData).to.exist.not.null;
82+
expect(socialData.socialUserId).to.exist.to.equal('123456');
83+
// TODO cross check population of username for all networks
84+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
85+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
86+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
87+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
88+
expect(socialData.socialProvider).to.exist.to.equal('github');
89+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
90+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
91+
});
92+
93+
it("should get JSON for github user data without lastname ", function() {
94+
mockProfile.identities[0].connection = 'github';
95+
mockProfile.name = 'mock';
96+
var socialData = Helpers.getSocialUserData(mockProfile, "");
97+
expect(socialData).to.exist.not.null;
98+
expect(socialData.socialUserId).to.exist.to.equal('123456');
99+
// TODO cross check population of username for all networks
100+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
101+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
102+
expect(socialData.lastname).to.exist.to.equal('');
103+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
104+
expect(socialData.socialProvider).to.exist.to.equal('github');
105+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
106+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
107+
});
108+
109+
it("should get JSON for bitbucket user data ", function() {
110+
mockProfile.identities[0].connection = 'bitbucket';
111+
var socialData = Helpers.getSocialUserData(mockProfile, "");
112+
expect(socialData).to.exist.not.null;
113+
expect(socialData.socialUserId).to.exist.to.equal('123456');
114+
// TODO cross check population of username for all networks
115+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
116+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
117+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
118+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
119+
expect(socialData.socialProvider).to.exist.to.equal('bitbucket');
120+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
121+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
122+
});
123+
124+
it("should get JSON for stackoverflow user data ", function() {
125+
mockProfile.identities[0].connection = 'stackoverflow';
126+
var socialData = Helpers.getSocialUserData(mockProfile, "");
127+
expect(socialData).to.exist.not.null;
128+
expect(socialData.socialUserId).to.exist.to.equal('123456');
129+
// TODO cross check population of username for all networks
130+
expect(socialData.username).to.exist.to.equal('123456');
131+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
132+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
133+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
134+
expect(socialData.socialProvider).to.exist.to.equal('stackoverflow');
135+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
136+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
137+
});
138+
139+
it("should get JSON for dribbble user data ", function() {
140+
mockProfile.identities[0].connection = 'dribbble';
141+
var socialData = Helpers.getSocialUserData(mockProfile, "");
142+
expect(socialData).to.exist.not.null;
143+
expect(socialData.socialUserId).to.exist.to.equal('123456');
144+
// TODO cross check population of username for all networks
145+
expect(socialData.username).to.exist.to.equal('123456');
146+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
147+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
148+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
149+
expect(socialData.socialProvider).to.exist.to.equal('dribbble');
150+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
151+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
152+
});
153+
154+
it("should get JSON for twitter user data ", function() {
155+
mockProfile.identities[0].connection = 'twitter';
156+
mockProfile.screen_name = mockProfile.username;
157+
var socialData = Helpers.getSocialUserData(mockProfile, "");
158+
expect(socialData).to.exist.not.null;
159+
expect(socialData.socialUserId).to.exist.to.equal('123456');
160+
// TODO cross check population of username for all networks
161+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
162+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
163+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
164+
// Twitter does not give email
165+
expect(socialData.email).to.exist.to.equal('');
166+
expect(socialData.socialProvider).to.exist.to.equal('twitter');
167+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
168+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
169+
});
170+
171+
it("should get JSON for twitter user data without lastname ", function() {
172+
mockProfile.identities[0].connection = 'twitter';
173+
mockProfile.name = 'mock';
174+
mockProfile.screen_name = mockProfile.username;
175+
var socialData = Helpers.getSocialUserData(mockProfile, "");
176+
expect(socialData).to.exist.not.null;
177+
expect(socialData.socialUserId).to.exist.to.equal('123456');
178+
// TODO cross check population of username for all networks
179+
expect(socialData.username).to.exist.to.equal(mockProfile.username);
180+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
181+
expect(socialData.lastname).to.exist.to.equal('');
182+
// Twitter does not give email
183+
expect(socialData.email).to.exist.to.equal('');
184+
expect(socialData.socialProvider).to.exist.to.equal('twitter');
185+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
186+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
187+
});
188+
189+
it("should get JSON for google-oauth2 user data ", function() {
190+
mockProfile.identities[0].connection = 'google-oauth2';
191+
var socialData = Helpers.getSocialUserData(mockProfile, "");
192+
expect(socialData).to.exist.not.null;
193+
expect(socialData.socialUserId).to.exist.to.equal('123456');
194+
// TODO cross check population of username for all networks
195+
expect(socialData.username).to.exist.to.equal(mockProfile.nickname);
196+
expect(socialData.firstname).to.exist.to.equal(mockProfile.first_name);
197+
expect(socialData.lastname).to.exist.to.equal(mockProfile.last_name);
198+
expect(socialData.email).to.exist.to.equal(mockProfile.email);
199+
expect(socialData.socialProvider).to.exist.to.equal('google-oauth2');
200+
expect(socialData.accessToken).to.exist.to.equal(mockProfile.identities[0].access_token);
201+
expect(socialData.accessTokenSecret).to.exist.to.equal(mockProfile.identities[0].access_token_secret);
202+
});
203+
});
204+
205+
describe("getPageTitle()", function() {
206+
207+
it("should get page title from state ", function() {
208+
var state = { data: {title: 'Mock Page'}};
209+
var title = Helpers.getPageTitle(state, null);
210+
expect(title).to.exist.to.equal('Mock Page | TopCoder');
211+
});
212+
213+
it("should get default page title when state does not have page title ", function() {
214+
var state = {};
215+
var title = Helpers.getPageTitle(state, null);
216+
expect(title).to.exist.to.equal('TopCoder');
217+
});
218+
219+
it("should get page title from state with dynamic data ", function() {
220+
var state = { data: {title: 'Mock Page {{a.b.c}}'}};
221+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {c: 'Title'}}}}}});
222+
expect(title).to.exist.to.equal('Mock Page Title | TopCoder');
223+
});
224+
225+
it("should get static page title from state with unknown expression for dynamic data ", function() {
226+
var state = { data: {title: 'Mock Page {a.b.c}'}};
227+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {c: 'Title'}}}}}});
228+
expect(title).to.exist.to.equal('Mock Page {a.b.c} | TopCoder');
229+
});
230+
231+
it("should replace dynamic data with empty value when not available in current state ", function() {
232+
var state = { data: {title: 'Mock Page {{a.b.c}}'}};
233+
var title = Helpers.getPageTitle(state, {locals : {resolve: {$$values : {a: {b : {}}}}}});
234+
expect(title).to.exist.to.equal('Mock Page | TopCoder');
235+
});
236+
});
57237
});

0 commit comments

Comments
 (0)