This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathtest.userActivationEmail.js
149 lines (134 loc) · 4.49 KB
/
test.userActivationEmail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.0
* @author Ghost_141
*/
'use strict';
/*global describe, it, before, beforeEach, after, afterEach */
/*jslint node: true, stupid: true, unparam: true */
/**
* Module dependencies.
*/
var _ = require('underscore');
var request = require('supertest');
var assert = require('chai').assert;
var async = require('async');
var testHelper = require('./helpers/testHelper');
var SQL_DIR = __dirname + '/sqls/userActivationEmail/';
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
describe('User Activation Email API', function () {
this.timeout(180000); // The api with testing remote db could be quit slow
var errorObject = require('../test/test_files/expected_user_activation_email_error_message'),
heffan = testHelper.generateAuthHeader({ sub: 'ad|132456' }),
user1 = testHelper.generateAuthHeader({ sub: 'ad|100326' }),
user2 = testHelper.generateAuthHeader({ sub: 'ad|100325' });
/**
* Clear database
* @param {Function<err>} done the callback
*/
function clearDb(done) {
async.waterfall([
function (cb) {
testHelper.runSqlFile(SQL_DIR + 'common_oltp__clean', 'common_oltp', cb);
}
], done);
}
/**
* This function is run before all tests.
* Generate tests data.
* @param {Function<err>} done the callback
*/
before(function (done) {
async.waterfall([
clearDb,
function (cb) {
testHelper.runSqlFile(SQL_DIR + 'common_oltp__insert_test_data', 'common_oltp', cb);
}
], done);
});
/**
* This function is run after all tests.
* Clean up all data.
* @param {Function<err>} done the callback
*/
after(function (done) {
clearDb(done);
});
/**
* create a http request and test it.
* @param {Number} expectStatus - the expected response status code.
* @param {Object} authHeader - the auth header.
* @param {Function} cb - the call back function.
*/
function createGetRequest(expectStatus, authHeader, cb) {
var req = request(API_ENDPOINT)
.get('/v2/user/activation-email/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/);
if (authHeader) {
req.set('Authorization', authHeader);
}
req.expect(expectStatus)
.end(cb);
}
/**
* assert the bad response.
* @param {Number} expectStatus - the expect status.
* @param {String} errorMessage - the expected error message.
* @param {Object} authHeader - the request auth header.
* @param {Function} cb - the callback function.
*/
function assertBadResponse(expectStatus, errorMessage, authHeader, cb) {
createGetRequest(expectStatus, authHeader, function (err, result) {
if (!err) {
assert.equal(result.body.error.details, errorMessage, 'invalid error message');
} else {
cb(err);
return;
}
cb();
});
}
/**
* Test when caller is anonymous.
*/
it('should return unauthorized Error. The caller is anonymous.', function (done) {
assertBadResponse(401, errorObject.unauthorized, null, done);
});
/**
* Test caller is an activated user.
*/
it('should return bad request. The challengeId is not number.', function (done) {
assertBadResponse(400, errorObject.alreadyActivated, heffan, done);
});
/**
* Test caller reach the resend limit.
*/
it('should return bad request. The user reach resend limit.', function (done) {
async.waterfall([
function (cb) {
async.timesSeries(5, function (time, next) {
createGetRequest(200, user2, function () {
next();
});
}, function (err) {
cb(err);
});
},
function (cb) {
assertBadResponse(400, errorObject.reachLimit, user2, cb);
}
], done);
});
it('should return success result.', function (done) {
createGetRequest(200, user1, function (err, res) {
if (err) {
done(err);
} else {
assert.isTrue(res.body.success, 'invalid response');
done();
}
});
});
});