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