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

Commit ae588a9

Browse files
author
fireice.topcoder
committed
Validate Social API
1 parent 672d802 commit ae588a9

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed

actions/memberRegistration.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,3 +1148,71 @@ exports.validateHandle = {
11481148
});
11491149
}
11501150
}; // validateHandle
1151+
1152+
/**
1153+
* The API for validate social.
1154+
*/
1155+
exports.validateSocial = {
1156+
name: "validateSocial",
1157+
description: "validateSocial",
1158+
inputs: {
1159+
required: ["socialProviderId", "socialUserId"],
1160+
optional: []
1161+
},
1162+
blockedConnectionTypes : [],
1163+
outputExample : {},
1164+
version : 'v2',
1165+
transaction : 'read',
1166+
databases : ["common_oltp"],
1167+
run: function (api, connection, next) {
1168+
var helper = api.helper,
1169+
socialProviderId = connection.params.socialProviderId,
1170+
socialUserId = connection.params.socialUserId,
1171+
dbConnectionMap = connection.dbConnectionMap,
1172+
result,
1173+
checkResult;
1174+
1175+
if (!dbConnectionMap) {
1176+
helper.handleNoConnection(api, connection, next);
1177+
return;
1178+
}
1179+
1180+
async.waterfall([
1181+
function (cb) {
1182+
checkResult = validateSocialProviderId(socialProviderId);
1183+
if (checkResult !== null) {
1184+
cb(new IllegalArgumentError(checkResult));
1185+
return;
1186+
}
1187+
isSoicalProviderIdValid(socialProviderId, api, dbConnectionMap, function (err, result) {
1188+
if (err) {
1189+
cb(err);
1190+
} else {
1191+
if (result !== true) {
1192+
cb(new BadRequestError("Social provider id is not valid."));
1193+
} else {
1194+
cb(null);
1195+
}
1196+
}
1197+
});
1198+
}, function(cb) {
1199+
checkResult = validateSocialUserId(socialUserId);
1200+
if (checkResult !== null) {
1201+
cb(new IllegalArgumentError(checkResult));
1202+
return;
1203+
}
1204+
isSoicalLoginExisted(socialProviderId, socialUserId, api, dbConnectionMap, function (err, existed) {
1205+
result = { available: existed };
1206+
cb(err);
1207+
});
1208+
}
1209+
], function (err) {
1210+
if (err) {
1211+
helper.handleError(api, connection, err);
1212+
} else {
1213+
connection.response = result;
1214+
}
1215+
next(connection, true);
1216+
});
1217+
}
1218+
}; // validateSocial

apiary.apib

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,43 @@ Register a new user.
985985
"value":"503",
986986
"description":"Servers are up but overloaded. Try again later."
987987
}
988+
989+
## Validate Social [/users/validateSocial?socialProviderId={socialProviderId}&socialUserId={socialUserId}]
990+
### Validate Handle [GET]
991+
992+
+ Parameters
993+
+ socialProviderId (required, string, `1`) ... social provider id
994+
+ socialUserId (required, string, `fb124764`) ... social user id
995+
996+
+ Response 200 (application/json)
997+
998+
{
999+
"available":true
1000+
}
1001+
1002+
+ Response 400 (application/json)
1003+
1004+
{
1005+
"name":"Bad Request",
1006+
"value":"400",
1007+
"description": "The request was invalid. An accompanying message will explain why."
1008+
}
1009+
1010+
+ Response 500 (application/json)
1011+
1012+
{
1013+
"name":"Internal Server Error",
1014+
"value":"500",
1015+
"description":"Unknown server error. Please contact support."
1016+
}
1017+
1018+
+ Response 503 (application/json)
1019+
1020+
{
1021+
"name":"Service Unavailable",
1022+
"value":"503",
1023+
"description":"Servers are up but overloaded. Try again later."
1024+
}
9881025

9891026
## Generate Reset Token [/users/resetToken/?handle={handle}&email={email}]
9901027
### Generate Reset Token [GET]

routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ exports.routes = {
153153

154154
{ path: "/:apiVersion/users/validateEmail", action: "emailValidation" },
155155
{ path: "/:apiVersion/users/validate/:handle", action: "validateHandle" },
156+
{ path: "/:apiVersion/users/validateSocial", action: "validateSocial" },
157+
156158
{ path: "/:apiVersion/users/search", action: "searchUsers" },
157159
{ path: "/:apiVersion/users/:handle/statistics/develop", action: "getSoftwareStatistics" },
158160
{ path: "/:apiVersion/users/:handle/statistics/design/recentWins", action: "getRecentWinningDesignSubmissions" },
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
delete from user_role_xref where user_role_id = 654001;
2+
delete from user_social_login where user_id IN (124764, 124772, 124766, 124776, 124834, 132456, 124852, 124835, 124853, 124856, 124857, 124861, 124916);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124764', 124764, 1, 'user124764');
2+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124772', 124772, 1, 'user124772');
3+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124766', 124766, 1, 'user124766');
4+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124776', 124776, 1, 'user124776');
5+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124834', 124834, 1, 'user124834');
6+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124852', 124852, 1, 'user124852');
7+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124835', 124835, 1, 'user124835');
8+
9+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124853', 124853, 1, 'user124853');
10+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124856', 124856, 1, 'user124856');
11+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124857', 124857, 1, 'user124857');
12+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124861', 124861, 1, 'user124861');
13+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb124916', 124916, 1, 'user124916');
14+
15+
INSERT INTO user_social_login(social_user_id, user_id, social_login_provider_id, social_user_name) VALUES ('fb132456', 132456, 1, 'user132456');
16+
17+
-- lightspeed is a studio admin
18+
INSERT INTO user_role_xref(user_role_id,login_id,role_id,create_user_id,security_status_id) VALUES (654001, 124834, 2040, 132458, 1);

test/test.validateSocial.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
3+
*
4+
* @version 1.0
5+
* @author TCSASSEMBLER
6+
*/
7+
"use strict";
8+
/*global describe, it, before, beforeEach, after, afterEach */
9+
/*jslint node: true, stupid: true, unparam: true */
10+
11+
/**
12+
* Module dependencies.
13+
*/
14+
var request = require('supertest');
15+
var assert = require('chai').assert;
16+
var async = require('async');
17+
var testHelper = require('./helpers/testHelper');
18+
var SQL_DIR = __dirname + "/sqls/validateSocial/";
19+
20+
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
21+
22+
describe('Test Bugs API', function () {
23+
this.timeout(30000); // The api with testing remote db could be quit slow
24+
25+
/**
26+
* Clear database
27+
* @param {Function<err>} done the callback
28+
*/
29+
function clearDb(done) {
30+
testHelper.runSqlFile(SQL_DIR + "common_oltp__clean", "common_oltp", done);
31+
}
32+
33+
/**
34+
* This function is run before all tests.
35+
* Generate tests data.
36+
* @param {Function<err>} done the callback
37+
*/
38+
before(function (done) {
39+
async.waterfall([
40+
function (cb) {
41+
clearDb(cb);
42+
}, function (cb) {
43+
testHelper.runSqlFile(SQL_DIR + "common_oltp__insert_test_data", "common_oltp", cb);
44+
}
45+
], done);
46+
});
47+
48+
/**
49+
* This function is run after all tests.
50+
* Clean up all data.
51+
* @param {Function<err>} done the callback
52+
*/
53+
after(function (done) {
54+
clearDb(done);
55+
});
56+
57+
/**
58+
* Get response and assert response from /api/v2/bugs/
59+
* @param {String} socialProviderId the social provider id
60+
* @param {String} socialUserId the social user id
61+
* @param {String} result the result
62+
* @param {Function<err>} done the callback
63+
*/
64+
function assertResponse(socialProviderId, socialUserId, result, done) {
65+
request(API_ENDPOINT)
66+
.get('/api/v2/users/validateSocial?socialProviderId='
67+
+ socialProviderId
68+
+ '&socialUserId='
69+
+ socialUserId)
70+
.set('Accept', 'application/json')
71+
.expect(200)
72+
.end(function (err, res) {
73+
assert.ifError(err);
74+
assert.ok(res.body);
75+
assert.equal(res.body.available, result);
76+
done(err);
77+
});
78+
}
79+
80+
/**
81+
* Get response and assert response from /api/v2/bugs/
82+
* @param {Number} statusCode the expected status code
83+
* @param {String} socialProviderId the social provider id
84+
* @param {String} socialUserId the social user id
85+
* @param {String} errorMessage the expected error message. Optional
86+
* @param {Function<err>} done the callback
87+
*/
88+
function assertErrorResponse(statusCode, socialProviderId, socialUserId, errorMessage, done) {
89+
request(API_ENDPOINT)
90+
.get('/api/v2/users/validateSocial?socialProviderId='
91+
+ socialProviderId
92+
+ '&socialUserId='
93+
+ socialUserId)
94+
.set('Accept', 'application/json')
95+
.expect(statusCode)
96+
.end(function (err, res) {
97+
if (err) {
98+
done(err);
99+
return;
100+
}
101+
if (errorMessage) {
102+
if (statusCode != 200) {
103+
assert.ok(res.body);
104+
assert.ok(res.body.error);
105+
assert.equal(res.body.error.details, errorMessage);
106+
} else if (statusCode === 200) {
107+
assert.ok(res.body);
108+
assert.ok(res.body.error);
109+
assert.equal(res.body.error, errorMessage);
110+
}
111+
}
112+
done();
113+
});
114+
}
115+
116+
/**
117+
* Test /api/v2/users/validateSocial?socialProviderId=&socialUserId=
118+
*/
119+
it('should return error if status is invalid value', function (done) {
120+
assertErrorResponse(200, '', '', 'Error: socialProviderId is a required parameter for this action', done);
121+
});
122+
123+
/**
124+
* Test /api/v2/users/validateSocial?socialProviderId=&socialUserId=a
125+
*/
126+
it('should return error if status is invalid value', function (done) {
127+
assertErrorResponse(200, '', 'a', 'Error: socialProviderId is a required parameter for this action', done);
128+
});
129+
130+
/**
131+
* Test /api/v2/users/validateSocial?socialProviderId=a&socialUserId=
132+
*/
133+
it('should return error if status is invalid value', function (done) {
134+
assertErrorResponse(200, 'a', '', 'Error: socialUserId is a required parameter for this action', done);
135+
});
136+
137+
/**
138+
* Test /api/v2/users/validateSocial?socialProviderId=-1&socialUserId=a
139+
*/
140+
it('should return error if status is invalid value', function (done) {
141+
assertErrorResponse(400, '-1', 'a', 'Social Provider ID must be integer', done);
142+
});
143+
144+
/**
145+
* Test /api/v2/users/validateSocial?socialProviderId=a&socialUserId=a
146+
*/
147+
it('should return error if status is invalid value', function (done) {
148+
assertErrorResponse(400, 'a', 'a', 'Social Provider ID must be integer', done);
149+
});
150+
151+
/**
152+
* Test /api/v2/users/validateSocial?socialProviderId= &socialUserId=a
153+
*/
154+
it('should return error if status is invalid value', function (done) {
155+
assertErrorResponse(400, ' ', 'a', 'Social Provider ID must be integer', done);
156+
});
157+
158+
/**
159+
* Test /api/v2/users/validateSocial?socialProviderId=1000&socialUserId=a
160+
*/
161+
it('should return error if status is invalid value', function (done) {
162+
assertErrorResponse(400, '1000', 'a', 'Social provider id is not valid.', done);
163+
});
164+
165+
/**
166+
* Test /api/v2/users/validateSocial?socialProviderId=1&socialUserId=fb124764
167+
*/
168+
it('should return results', function (done) {
169+
assertResponse("1", "fb124764", true, done);
170+
});
171+
172+
/**
173+
* Test /api/v2/users/validateSocial?socialProviderId=1&socialUserId=fb124764a
174+
*/
175+
it('should return results', function (done) {
176+
assertResponse("1", "fb124764a", false, done);
177+
});
178+
});

0 commit comments

Comments
 (0)