-
Notifications
You must be signed in to change notification settings - Fork 4
/
clients-rest-tests.js
91 lines (84 loc) · 2.97 KB
/
clients-rest-tests.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
// Clients REST API
// -----------------------------
// Modules Dependencies:
// - Assert (http://nodejs.org/api/assert.html)
// - SuperAgent (http://visionmedia.github.io/superagent/)
var assert = require('assert'),
config = require('../../../config'),
superagent = require('superagent');
// Require basic config files and DB connection
var sequelize = exports.sequelize = require('../../../db/conn.js');
// Global Variables for the test case
var Client, client, agent, clientId, d;
d = 'http://'+config.app.domain+":"+config.app.port;
// Unit Tests
describe('REST API Client '+d+"/api/v1/clients", function(){
before(function(done){
// Before all tests
Client = require("../../../models/clients.js");
// Get domain
d = config.app.domain+":"+config.app.port;
// Start agent
agent = superagent.agent();
// It show create a new document in the database
client = Client.build({ name: 'client'+Math.floor((Math.random() * 10) + 1)});
client.save().then(function(record){
clientId = record.get('id');
done()
}).catch(function(error) {
done(error);
});
});
describe('Clients REST', function(){
it('GET /api/v1/clients', function(done){
agent
.get(d+'/api/v1/clients')
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.length>0);
done();
});
});
it('GET /api/v1/clients?count=1', function(done){
agent
.get(d+'/api/v1/clients?count=1')
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.length === 1);
done();
});
});
it('POST /api/v1/clients', function(done){
agent
.post(d+'/api/v1/clients')
.send({ name: 'Test Creation Client' })
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.name === 'Test Creation Client');
done();
});
});
it('PUT /api/v1/clients/:clientId', function(done){
console.log("Client ID:", clientId);
agent
.put(d+'/api/v1/clients/'+clientId)
.send({ name: 'Test Change Client' })
.end(function(res) {
assert.ok(res.ok);
console.log("PUT: ", res.body);
assert.ok(res.body.name === 'Test Change Client');
done();
});
});
it('DELETE /api/v1/clients/:clientId', function(done){
agent
.del(d+'/api/v1/clients/'+clientId)
.end(function(res) {
assert.ok(res.ok);
console.log(res.body);
assert.ok(JSON.stringify(res.body) === '{}');
done();
});
});
});
});