-
Notifications
You must be signed in to change notification settings - Fork 4
/
providers-rest-tests.js
91 lines (84 loc) · 3.04 KB
/
providers-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
// Providers 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 Provider, provider, agent, providerId, d;
d = 'http://'+config.app.domain+":"+config.app.port;
// Unit Tests
describe('REST API Provider '+d+"/api/v1/providers", function(){
before(function(done){
// Before all tests
Provider = require("../../../models/providers.js");
// Get domain
d = config.app.domain+":"+config.app.port;
// Start agent
agent = superagent.agent();
// It show create a new document in the database
provider = Provider.build({ name: 'provider'+Math.floor((Math.random() * 10) + 1)});
provider.save().then(function(record){
providerId = record.get('id');
done()
}).catch(function(error) {
done(error);
});
});
describe('Providers REST', function(){
it('GET /api/v1/providers', function(done){
agent
.get(d+'/api/v1/providers')
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.length>0);
done();
});
});
it('GET /api/v1/providers?count=1', function(done){
agent
.get(d+'/api/v1/providers?count=1')
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.length === 1);
done();
});
});
it('POST /api/v1/providers', function(done){
agent
.post(d+'/api/v1/providers')
.send({ name: 'Test Creation Provider' })
.end(function(res) {
assert.ok(res.ok);
assert.ok(res.body.name === 'Test Creation Provider');
done();
});
});
it('PUT /api/v1/providers/:providerId', function(done){
console.log("Provider ID:", providerId);
agent
.put(d+'/api/v1/providers/'+providerId)
.send({ name: 'Test Change Provider' })
.end(function(res) {
assert.ok(res.ok);
console.log("PUT: ", res.body);
assert.ok(res.body.name === 'Test Change Provider');
done();
});
});
it('DELETE /api/v1/providers/:providerId', function(done){
agent
.del(d+'/api/v1/providers/'+providerId)
.end(function(res) {
assert.ok(res.ok);
console.log(res.body);
assert.ok(JSON.stringify(res.body) === '{}');
done();
});
});
});
});