-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest.js
226 lines (188 loc) · 6.17 KB
/
test.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict'
/* globals describe beforeEach it afterEach */
const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
const nock = require('nock')
nock.disableNetConnect()
const mock = require('mock-fs')
const rewire = require('rewire')
// const fs = require('fs')
// heroku stuff
const cli = require('heroku-cli-util')
const commands = require('./index').commands
// things I'm testing
const merge = require('./util/merge')
const file = rewire('./util/file')
const header = file.__get__('header')
const fs = require('fs')
// HELPERS
function fetchCMD (name) {
return commands.find((c) => c.command === name)
}
function setup () {
cli.raiseErrors = true
}
function defaultFS () {
// this is so I can setup without affecting other tests
return {
'.env': fixtures.local_file,
'other.txt': fixtures.local_file,
'dnt': mock.file({mode: '000'})
}
}
// FIXTURES
const fixtures = {
remote_obj: {
NODE_ENV: 'production',
NAME: 'david',
SOURCE: 'remote'
}, local_obj: {
NODE_ENV: 'test',
SOURCE: 'local',
DB_STRING: 'mongo://[email protected]:4567'
}, remote_win_obj: {
NODE_ENV: 'production',
SOURCE: 'remote',
DB_STRING: 'mongo://[email protected]:4567',
NAME: 'david'
}, local_win_obj: {
NODE_ENV: 'test',
SOURCE: 'local',
DB_STRING: 'mongo://[email protected]:4567',
NAME: 'david'
},
local_file: '#comment\nNODE_ENV=test\nSOURCE=local\nSOURCE=local\nDB_STRING=mongo://[email protected]:4567\n',
merged_local_file: header + 'DB_STRING="mongo://[email protected]:4567"\nNAME="david"\nNODE_ENV="test"\nSOURCE="local"\n',
// test both quote styles
sample_file: header + 'PIZZA="Abo\'s"\nNAME="david"\n\n#this is a comment!\nCITY=boulder\n\n\n',
clean_sample_file: header + 'CITY="boulder"\nNAME="david"\nPIZZA="Abo\'s"\n',
sample_obj: { NAME: 'david', CITY: 'boulder', PIZZA: "Abo's" }
}
// TESTS
setup()
describe('Merging', () => {
it('should overwrite local with remote', () => {
expect(merge(fixtures.local_obj, fixtures.remote_obj, {})).to.deep.equal(fixtures.remote_win_obj)
})
it('should overwrite remote with local', () => {
expect(merge(fixtures.remote_obj, fixtures.local_obj, {})).to.deep.equal(fixtures.local_win_obj)
})
it('should overwrite local with remote w/override', () => {
expect(merge(fixtures.remote_obj, fixtures.local_obj, {overwrite: true})).to.deep.equal(fixtures.remote_win_obj)
})
it('should overwrite remote with local w/override', () => {
expect(merge(fixtures.local_obj, fixtures.remote_obj, {overwrite: true})).to.deep.equal(fixtures.local_win_obj)
})
})
describe('Reading', () => {
beforeEach(() => {
mock(defaultFS())
cli.mockConsole()
})
it('should return empty object from non-existant file', () => {
return expect(file.read('asdf')).to.eventually.deep.equal({})
})
it('should read a local file', () => {
return expect(file.read('.env')).to.eventually.deep.equal(fixtures.local_obj)
})
it('should warn about duplicate keys', () => {
return file.read('.env').then(() => {
expect(cli.stderr).to.include('"SOURCE" is in env file twice')
})
})
it('should skip warnings in quiet mode', () => {
return file.read('.env', true).then(() => {
expect(cli.stderr).to.equal('')
})
})
afterEach(() => {
mock.restore()
})
})
describe('Writing', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
it('should fail to read from a file it lacks permissions for', () => {
return expect(file.write({}, 'dnt')).to.be.rejectedWith(Error)
})
let fname = '.env'
it('should successfully write a file, louldly', () => {
return file.write(fixtures.sample_obj).then(() => {
let res = fs.readFileSync(fname, 'utf-8')
expect(res).to.equal(fixtures.clean_sample_file)
expect(cli.stdout).to.not.equal('')
})
})
it('should successfully write a file, quietly', () => {
// need to pass all params if i'm passing quiet mode
return file.write(fixtures.sample_obj, fname, true).then(() => {
let res = fs.readFileSync(fname, 'utf-8')
expect(res).to.equal(fixtures.clean_sample_file)
expect(cli.stdout).to.equal('')
})
})
afterEach(() => {
mock.restore()
})
})
describe('Transforming', () => {
it('should decode files correctly', () => {
expect(file.__get__('objFromFileFormat')(fixtures.sample_file)).to.deep.equal(fixtures.sample_obj)
})
it('should encode file correctly', () => {
expect(file.__get__('objToFileFormat')(fixtures.sample_obj)).to.equal(fixtures.clean_sample_file)
})
})
describe('Pushing', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
it('should correctly push configs w/ flags', () => {
nock('https://api.heroku.com:443')
.get('/apps/test/config-vars')
.reply(200, fixtures.remote_obj)
// this will fail if we don't pass the correct body, as intended
nock('https://api.heroku.com:443')
.patch('/apps/test/config-vars', fixtures.remote_win_obj)
.reply(200, fixtures.remote_win_obj)
// fetch the updated value
nock('https://api.heroku.com:443')
.get('/apps/test/config-vars')
.reply(200, fixtures.remote_win_obj)
let cmd = fetchCMD('push')
let fname = 'other.txt'
return cmd.run({flags: { file: fname, quiet: true }, app: 'test'}).then(() => {
return cli.got('https://api.heroku.com:443/apps/test/config-vars')
}).then((res) => {
expect(JSON.parse(res.body)).to.deep.equal(fixtures.remote_win_obj)
expect(cli.stdout).to.equal('')
})
})
afterEach(() => {
mock.restore()
})
})
describe('Pulling', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
it('should correctly pull configs', () => {
nock('https://api.heroku.com:443')
.get('/apps/test/config-vars')
.reply(200, fixtures.remote_obj)
let cmd = fetchCMD('pull')
let fname = 'other.txt'
return cmd.run({flags: {file: fname}, app: 'test'}).then(() => {
let res = fs.readFileSync(fname, 'utf-8')
expect(res).to.include(fixtures.merged_local_file)
})
})
afterEach(() => {
mock.restore()
})
})