forked from robburger/citizen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.spec.js
150 lines (131 loc) · 5.28 KB
/
modules.spec.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
150
const request = require('supertest');
const { expect } = require('chai');
const { promisify } = require('util');
const rimraf = promisify(require('rimraf'));
const fs = require('fs');
const path = require('path');
const mkdirp = promisify(require('mkdirp'));
const app = require('../app');
const { deleteDbAll } = require('../test/helper');
const { db, save } = require('../lib/store');
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
describe('POST /v1/modules/:namespace/:name/:provider/:version', () => {
let moduleBuf;
let modulePath = `hashicorp/consul/aws/${(new Date()).getTime()}`;
const tarballPath = path.join(__dirname, '../test', 'fixture/test.tar.gz');
beforeEach(async () => {
modulePath = `hashicorp/consul/aws/${(new Date()).getTime()}`;
});
afterEach(async () => {
await deleteDbAll(db);
await rimraf(process.env.CITIZEN_STORAGE_PATH);
});
it('should register new module', () => request(app)
.post(`/v1/modules/${modulePath}`)
.attach('module', 'test/fixture/module.tar.gz')
.expect('Content-Type', /application\/json/)
.expect(201)
.then((res) => {
expect(res.body).to.have.property('modules').to.be.an('array');
expect(res.body.modules[0]).to.have.property('id').to.equal(modulePath);
}));
it('should reject the reqeust if the module is already exists.', async () => {
const pathToStore = path.join(process.env.CITIZEN_STORAGE_PATH, `${modulePath}/test.tar.gz`);
const parsedPath = path.parse(pathToStore);
await mkdirp(parsedPath.dir);
await writeFile(pathToStore, moduleBuf);
moduleBuf = await readFile(tarballPath);
request(app)
.post(`/v1/modules/${modulePath}`)
.attach('module', 'test/fixture/test.tar.gz')
.expect('Content-Type', /application\/json/)
.expect(409)
.then((res) => {
expect(res.body).to.have.property('errors').to.be.an('array');
});
});
it('should register new module with owner infomation', () => request(app)
.post(`/v1/modules/${modulePath}`)
.field('owner', 'outsideris')
.attach('module', 'test/fixture/module.tar.gz')
.expect('Content-Type', /application\/json/)
.expect(201)
.then((res) => {
expect(res.body).to.have.property('modules').to.be.an('array');
expect(res.body.modules[0]).to.have.property('id').to.equal(modulePath);
}));
it('should register module information', (done) => {
request(app)
.post(`/v1/modules/${modulePath}`)
.attach('module', 'test/fixture/complex.tar.gz')
.expect('Content-Type', /application\/json/)
.expect(201)
.then((res) => {
db.find({
namespace: res.body.modules[0].namespace,
name: res.body.modules[0].name,
provider: res.body.modules[0].provider,
version: res.body.modules[0].version,
}, (err, docs) => {
if (err) { return done(err); }
expect(docs[0]).to.have.property('root');
expect(docs[0].root).to.have.property('name').to.equal('consul');
expect(docs[0]).to.have.property('submodules').to.be.an.instanceof(Array);
expect(docs[0].submodules).to.have.lengthOf(3);
return done();
});
});
});
});
describe('GET /v1/modules/:namespace/:name/:provider/:version', () => {
before(async () => {
await save({
namespace: 'router', name: 'specific', provider: 'aws', version: '1.1.2', owner: '',
});
});
after(async () => {
await deleteDbAll(db);
});
it('should return a specific module', () => request(app)
.get('/v1/modules/router/specific/aws/1.1.2')
.expect('Content-Type', /application\/json/)
.expect(200)
.then((res) => {
expect(res.body).to.have.property('id').to.equal('router/specific/aws/1.1.2');
expect(res.body).to.have.property('version').to.equal('1.1.2');
}));
it('should return 404 if given module does not exist', () => request(app)
.get('/v1/modules/router/specific/aws/2.1.2')
.expect('Content-Type', /application\/json/)
.expect(404));
});
describe('GET /v1/modules/:namespace/:name/:provider', () => {
before(async () => {
await save({
namespace: 'router', name: 'latest', provider: 'aws', version: '1.1.1', owner: '', definition: { root: { name: 'latest' }, submodules: [{ name: 'example' }] },
});
await save({
namespace: 'router', name: 'latest', provider: 'aws', version: '1.1.2', owner: '', definition: { root: { name: 'latest' }, submodules: [{ name: 'example' }] },
});
});
after(async () => {
await deleteDbAll(db);
});
it('should return latest version for a specific module provider', () => request(app)
.get('/v1/modules/router/latest/aws')
.expect('Content-Type', /application\/json/)
.expect(200)
.then((res) => {
expect(res.body).to.have.property('version').to.equal('1.1.2');
expect(res.body).to.have.property('root').to.have.property('name').to.equal('latest');
expect(res.body.submodules[0]).to.have.property('name').to.equal('example');
}));
it('should return 404 if given module does not exist', () => request(app)
.get('/v1/modules/router/latest/nomodule')
.expect('Content-Type', /application\/json/)
.expect(404)
.then((res) => {
expect(res.body).to.have.property('errors').to.be.an('array');
}));
});