-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.test.js
161 lines (127 loc) · 4.92 KB
/
index.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
describe('initer', () => {
describe('getDefaultSectionString', () => {
test('returns string on input: empty string', () => {
const getDefaultSectionString = require('./index')._only_for_testing_getDefaultSectionString
const filecontents = ' '
const res = getDefaultSectionString(filecontents)
expect(typeof res).toBe('string')
expect(res.trim()).toBe('')
})
test('returns empty string on input: [default] header', () => {
const getDefaultSectionString = require('./index')._only_for_testing_getDefaultSectionString
const filecontents = `
[defaut]
`
const res = getDefaultSectionString(filecontents)
expect(typeof res).toBe('string')
expect(res.trim()).toBe('')
})
test('returns empty string on input: other header, other section', () => {
const getDefaultSectionString = require('./index')._only_for_testing_getDefaultSectionString
const filecontents = `
[some-other-header]
first
second
`
const res = getDefaultSectionString(filecontents)
expect(typeof res).toBe('string')
expect(res.trim()).toBe('')
})
test('returns section on input: [default] header and section', () => {
const getDefaultSectionString = require('./index')._only_for_testing_getDefaultSectionString
const filecontents = `
[default]
first
second
`
const res = getDefaultSectionString(filecontents)
expect(typeof res).toBe('string')
const text = res.trim() // 'first\nsecond'
expect(/first/.test(text)).toBe(true)
expect(/second/.test(text)).toBe(true)
})
test('returns section on input: other section 1, [default] header, section, other section 2', () => {
const getDefaultSectionString = require('./index')._only_for_testing_getDefaultSectionString
const filecontents = `
[some-other-profile-a]
sky
air
[default]
first
second
[some-other-profile-b]
clouds
`
const res = getDefaultSectionString(filecontents)
expect(typeof res).toBe('string')
const text = res.trim()
// We want all between [default] and next header, but nothing else
expect(text).toBe('first\nsecond')
})
})
describe('parseAwsCredentialsOrConfigFile', () => {
test('returns default credentials on just [default] section present', () => {
const parseAwsCredentialsOrConfigFile = require('./index')._only_for_testing_parseAwsCredentialsOrConfigFile
const filecontents = `
[default]
aws_access_key_id = AKIA2WOM6JAHXXXXXXXX
aws_secret_access_key = XXXXXXXXXX+tgppEZPzdN/XXXXlXXXXX/XXXXXXX
`
const res = parseAwsCredentialsOrConfigFile(filecontents)
expect(res).toBeDefined()
expect(res.default).toBeDefined()
expect(res.default.aws_access_key_id).toBe('AKIA2WOM6JAHXXXXXXXX')
expect(res.default.aws_secret_access_key).toBe('XXXXXXXXXX+tgppEZPzdN/XXXXlXXXXX/XXXXXXX')
})
test('returns default credentials on multiple sections present', () => {
const parseAwsCredentialsOrConfigFile = require('./index')._only_for_testing_parseAwsCredentialsOrConfigFile
// the more weirdly formed
const filecontents = `
[some-other-section-a]
some-other-section-field-b=1234567890
[default]
aws_access_key_id= AKIA2WOM6JAHXXXXXXXX
aws_secret_access_key =XXXXXXXXXX+tgppEZPzdN/XXXXlXXXXX/XXXXXXX
[some-other-section-b]
some-other-section-field-b=098765434567898765
`
const res = parseAwsCredentialsOrConfigFile(filecontents)
expect(res).toBeDefined()
expect(res.default).toBeDefined()
expect(res.default.aws_access_key_id).toBe('AKIA2WOM6JAHXXXXXXXX')
expect(res.default.aws_secret_access_key).toBe('XXXXXXXXXX+tgppEZPzdN/XXXXlXXXXX/XXXXXXX')
})
})
describe('init', () => {
// TODO create mock .aws and see if fields are extracted correctly
test('runs, and output has expected structure', async () => {
const os = require('os')
const uuidv4 = require('uuid').v4
const fs = require('fs')
const path = require('path')
const { init } = require('./index')
// init will write hyperform.json here
const absdir = path.join(os.tmpdir(), uuidv4())
fs.mkdirSync(absdir)
let err
try {
init(absdir)
} catch (e) {
console.log(e)
err = e
}
// it didn't throw
expect(err).not.toBeDefined()
// it wrote hyperform.json
const hyperformJsonPath = path.join(absdir, 'hyperform.json')
expect(fs.existsSync(hyperformJsonPath)).toBe(true)
// hyperform.json has the expected structure
let hyperformJson = fs.readFileSync(hyperformJsonPath)
hyperformJson = JSON.parse(hyperformJson)
expect(hyperformJson.amazon).toBeDefined()
expect(hyperformJson.amazon.aws_access_key_id).toBeDefined()
expect(hyperformJson.amazon.aws_secret_access_key).toBeDefined()
expect(hyperformJson.amazon.aws_region).toBeDefined()
})
})
})