eitri-cli 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +54 -0
  2. package/bitbucket-pipelines.toBeChanged.yml +64 -0
  3. package/boilerplate/mini-app-cafe-madeira.zip +0 -0
  4. package/check-version.js +12 -0
  5. package/config/default-eitri.js +1 -0
  6. package/config/dev.js +91 -0
  7. package/config/k8s-eitri.js +92 -0
  8. package/config/loc-eitri.js +92 -0
  9. package/config/prod-eitri.js +92 -0
  10. package/config/test-eitri.js +2 -0
  11. package/index-eitri.js +6 -0
  12. package/index.js +182 -0
  13. package/install-dev.bat +1 -0
  14. package/install-dev.sh +1 -0
  15. package/jest.config.js +6 -0
  16. package/jsconfig.json +9 -0
  17. package/package.json +80 -0
  18. package/publisher.js +53 -0
  19. package/src/cmd/clean.js +179 -0
  20. package/src/cmd/create.js +281 -0
  21. package/src/cmd/credentials.js +105 -0
  22. package/src/cmd/invite.js +87 -0
  23. package/src/cmd/list.js +36 -0
  24. package/src/cmd/login.js +51 -0
  25. package/src/cmd/manage-env.js +129 -0
  26. package/src/cmd/open-share.js +6 -0
  27. package/src/cmd/order-details.js +6 -0
  28. package/src/cmd/push-version.js +182 -0
  29. package/src/cmd/show-message-if-outdated-package.js +32 -0
  30. package/src/cmd/signup.js +69 -0
  31. package/src/cmd/start.js +171 -0
  32. package/src/cmd/tail-logs.js +26 -0
  33. package/src/cmd/test-initialization-params.js +6 -0
  34. package/src/cmd/validate.js +170 -0
  35. package/src/cmd/version.js +29 -0
  36. package/src/enum/WatcherOpts.js +3 -0
  37. package/src/enum/target.js +6 -0
  38. package/src/helpers/request-listener-helper.js +55 -0
  39. package/src/model/Payload.js +45 -0
  40. package/src/model/Target.js +76 -0
  41. package/src/model/User.js +11 -0
  42. package/src/service/AuthConfig.js +88 -0
  43. package/src/service/BlindGuardian.js +134 -0
  44. package/src/service/CliLogin.js +47 -0
  45. package/src/service/ConfigService.js +16 -0
  46. package/src/service/CredentialsService.js +47 -0
  47. package/src/service/GATrackingStrategy.js +16 -0
  48. package/src/service/HashFolder.js +79 -0
  49. package/src/service/Http.js +234 -0
  50. package/src/service/InviteService.js +45 -0
  51. package/src/service/ManageEnvService.js +10 -0
  52. package/src/service/MiniLog.js +132 -0
  53. package/src/service/QRCodeFactory.js +43 -0
  54. package/src/service/Server.js +239 -0
  55. package/src/service/StarterService.js +31 -0
  56. package/src/service/TagTree.js +101 -0
  57. package/src/service/TargetService.js +97 -0
  58. package/src/service/TrackService.js +11 -0
  59. package/src/service/TrackingEitriAnalytics.js +32 -0
  60. package/src/service/TrackingService.js +183 -0
  61. package/src/service/ValidateResult.js +57 -0
  62. package/src/service/Watcher.js +119 -0
  63. package/src/service/Workspace.js +1069 -0
  64. package/src/service/WorkspaceManager.js +74 -0
  65. package/src/service/factories/DoubtsStarterFactory.js +25 -0
  66. package/src/service/factories/MiniWebAppFactory.js +43 -0
  67. package/src/service/factories/QRCodeStarterFactory.js +78 -0
  68. package/src/service/factories/WebStarterFactory.js +88 -0
  69. package/src/service/factories/WoodCoffeeFactory.js +230 -0
  70. package/src/util/AboutTemplate.jsx +14 -0
  71. package/src/util/UrlUtils.js +12 -0
  72. package/src/util/UserLocalCredential.js +122 -0
  73. package/src/util/error-messages/error-messages.js +24 -0
  74. package/src/util/error-messages/invite/invite-error-messages.js +14 -0
  75. package/src/util/getCliVersion.js +13 -0
  76. package/src/util/getCreateFactory.js +9 -0
  77. package/src/util/ipv4.js +19 -0
  78. package/src/util/manage-env.js +65 -0
  79. package/src/util/open-docs.js +7 -0
  80. package/src/util/os.js +39 -0
  81. package/src/util/server-url.js +15 -0
  82. package/src/util/template-utils.js +21 -0
  83. package/src/view/index.html +19 -0
  84. package/targetMobileStickyness.md +47 -0
  85. package/targetWebStickyness.md +40 -0
  86. package/test/_fixtures/factory.js +30 -0
  87. package/test/_fixtures/miniWebApp/miniapp.conf.js +4 -0
  88. package/test/_fixtures/miniapp.conf.js +5 -0
  89. package/test/_fixtures/server/HelloWorldBackend.js +7 -0
  90. package/test/_fixtures/src/Home.js +5 -0
  91. package/test/_fixtures/src/Home2.js +5 -0
  92. package/test/_fixtures/src/commons/util.js +3 -0
  93. package/test/_fixtures/src/components/TagA.jsx +4 -0
  94. package/test/_fixtures/src/components/TagB.jsx +4 -0
  95. package/test/_fixtures/src/components/TagC.jsx +3 -0
  96. package/test/_fixtures/src/components/TagD.jsx +3 -0
  97. package/test/_fixtures/src/server/foo.js +7 -0
  98. package/test/_fixtures/src/views/AboutTemplate.jsx +14 -0
  99. package/test/_fixtures/woodcoffee/miniapp.conf.js +3 -0
  100. package/test/ame.conf.js +3 -0
  101. package/test/cmd/clean.test.js +66 -0
  102. package/test/cmd/create.test.js +252 -0
  103. package/test/cmd/credentials.test.js +159 -0
  104. package/test/cmd/list.test.js +74 -0
  105. package/test/cmd/manage-env.test.js +168 -0
  106. package/test/cmd/signup.test.js +20 -0
  107. package/test/cmd/start.test.js +5 -0
  108. package/test/miniapp.conf.js +3 -0
  109. package/test/model/Payload.test.js +35 -0
  110. package/test/service/BlindGuardian.test.js +84 -0
  111. package/test/service/CheckAmeConf.test.js +313 -0
  112. package/test/service/Http.test.js +312 -0
  113. package/test/service/InviteService.test.js +117 -0
  114. package/test/service/MiniWebAppFactory.test.js +40 -0
  115. package/test/service/TagTree.test.js +81 -0
  116. package/test/service/TargetService.test.js +48 -0
  117. package/test/service/TrackingService.test.js +105 -0
  118. package/test/service/UserAmeConf.test.js +47 -0
  119. package/test/service/WoodCoffeeFactory.test.js +148 -0
  120. package/test/service/Workspace.test.js +364 -0
  121. package/thinQrCode.md +58 -0
  122. package/v1.5.0.md +3 -0
@@ -0,0 +1,252 @@
1
+ /* eslint-disable no-undef */
2
+ const fs = require('fs')
3
+ const path = require('path')
4
+ const childProcess = require('child_process')
5
+ const create = require('../../src/cmd/create')
6
+ const nock = require('nock')
7
+ const inquirer = require('inquirer')
8
+ const {faker} = require('@faker-js/faker')
9
+ const WookCoffee = require('../../src/service/factories/WoodCoffeeFactory')
10
+ const getCreateFactory = require('../../src/util/getCreateFactory')
11
+ const {getTarget} = require('../_fixtures/factory')
12
+ const {workspace} = require('../../src/service/Workspace')
13
+ jest.setTimeout(15000)
14
+
15
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
16
+
17
+ jest.mock('../../src/util/getCreateFactory', () => jest.fn())
18
+
19
+ describe('create', () => {
20
+ xit('cria o mini app no backend ao rodar create', async () => {
21
+ const env = Object.create(process.env)
22
+
23
+ env.NODE_ENV = 'lab'
24
+ console.log(env.NODE_APP_INSTANCE)
25
+ childProcess.execSync('rm -rf ./apagar-pasta')
26
+ child = childProcess.spawn(
27
+ 'node',
28
+ ['index.js', 'create', 'apagar-pasta'],
29
+ { env: env }
30
+ )
31
+
32
+ child.stdin.setEncoding('utf8')
33
+ child.stdout.pipe(process.stdout)
34
+ await delay(500)
35
+ child.stdin.write(`Nome-${Date.now()}+1\n`)
36
+ await delay(500)
37
+ child.stdin.write(`titulo-${Date.now()}\n`)
38
+ await delay(500)
39
+ child.stdin.write(`slug-${Date.now()}\n`)
40
+ await delay(500)
41
+ child.stdin.write('empresa\n')
42
+ await delay(7000)
43
+ child.stdin.end()
44
+
45
+ let content = fs.readFileSync(
46
+ path.join(__dirname, '../../apagar-pasta/miniapp.conf.js'),
47
+ 'utf8'
48
+ )
49
+ expect(content).toMatch(
50
+ /"public-key": "\w{8}-\w{4}-\w{4}-\w{4}-\w{12}"/
51
+ )
52
+ })
53
+
54
+ xit('trata nome ja existente', async () => {
55
+ const env = Object.create(process.env)
56
+
57
+ env.NODE_ENV = 'lab'
58
+ childProcess.execSync('rm -rf ./apagar-pasta')
59
+ child = childProcess.spawn(
60
+ 'node',
61
+ ['index.js', 'create', 'apagar-pasta'],
62
+ { env: env }
63
+ )
64
+ child.stdin.setEncoding('utf8')
65
+ child.stdout.pipe(process.stdout)
66
+ let allOutput = ''
67
+ child.stdout.on('data', function (data) {
68
+ data = data.toString()
69
+ allOutput += data
70
+ })
71
+
72
+ await delay(500)
73
+ child.stdin.write('Depositar\n')
74
+ await delay(500)
75
+ child.stdin.write(`titulo-${Date.now()}\n`)
76
+ await delay(500)
77
+ child.stdin.write(`slug-${Date.now()}\n`)
78
+ await delay(500)
79
+ child.stdin.write('empresa\n')
80
+ await delay(5000)
81
+ expect(allOutput).toContain('Outro nome')
82
+ await delay(500)
83
+ child.stdin.write(`Nome ${Date.now()}\n`)
84
+ console.log('ok')
85
+ await delay(5000)
86
+ child.stdin.end()
87
+ let content = fs.readFileSync(
88
+ path.join(__dirname, '../../apagar-pasta/miniapp.conf.js'),
89
+ 'utf8'
90
+ )
91
+ expect(content).toMatch(
92
+ /"public-key": "\w{8}-\w{4}-\w{4}-\w{4}-\w{12}"/
93
+ )
94
+ })
95
+
96
+ xit('trata slug ja existente', async () => {
97
+ const env = Object.create(process.env)
98
+
99
+ env.NODE_ENV = 'lab'
100
+ childProcess.execSync('rm -rf ./apagar-pasta')
101
+ child = childProcess.spawn(
102
+ 'node',
103
+ ['index.js', 'create', 'apagar-pasta'],
104
+ { env: env }
105
+ )
106
+ child.stdin.setEncoding('utf8')
107
+ child.stdout.pipe(process.stdout)
108
+ let allOutput = ''
109
+ child.stdout.on('data', function (data) {
110
+ data = data.toString()
111
+ allOutput += data
112
+ })
113
+
114
+ await delay(500)
115
+ child.stdin.write(`Nome ${Date.now()}\n`)
116
+ await delay(500)
117
+ child.stdin.write(`titulo-${Date.now()}\n`)
118
+ await delay(500)
119
+ child.stdin.write('transaction-cashin\n')
120
+ await delay(500)
121
+ child.stdin.write('empresa\n')
122
+ await delay(5000)
123
+ expect(allOutput).toContain('Outro slug')
124
+ await delay(500)
125
+ child.stdin.write(`slug-${Date.now()}\n`)
126
+ console.log('ok')
127
+ await delay(5000)
128
+ child.stdin.end()
129
+ let content = fs.readFileSync(
130
+ path.join(__dirname, '../../apagar-pasta/miniapp.conf.js'),
131
+ 'utf8'
132
+ )
133
+ expect(content).toMatch(
134
+ /"public-key": "\w{8}-\w{4}-\w{4}-\w{4}-\w{12}"/
135
+ )
136
+ })
137
+
138
+ describe('--target', () => {
139
+ const expectedTarget = getTarget()
140
+
141
+ // stub inquirer
142
+ let backup
143
+ beforeEach(async () => {
144
+ backup = inquirer.prompt
145
+ })
146
+
147
+ it('should succesfully show all available targets', async () => {
148
+
149
+ const targets = [expectedTarget, {...expectedTarget, name: 'TARGET_WHATEVER_2'}]
150
+ nock('https://dev.eitri.calindra.com.br')
151
+ .post('/blind-guardian-api/v1/o/auth')
152
+ .reply(200, { accessToken: 'xpto' })
153
+
154
+ nock('https://dev.eitri.calindra.com.br')
155
+ .get('/workspace/targets')
156
+ .matchHeader('Authorization', 'Bearer xpto')
157
+ .reply(200, targets)
158
+
159
+ inquirer.prompt = (questions) => {
160
+ // Checando se o que foi exibido para o usuario esta correto
161
+ expect(questions[0].choices).toMatchObject([
162
+ 'CALINDRA_MOBILE',
163
+ 'TARGET_WHATEVER_2',
164
+ 'Dúvidas? Veja documentação no browser',
165
+ ])
166
+
167
+ // Apos a montagem da lista, nao queremos mais que o codigo de negocio restante execute,
168
+ // pois ja identificamos que a lista foi exibida corretamente, que é o objetivo deste teste.
169
+ throw 'success'
170
+ }
171
+
172
+ const cmdObjStub = {
173
+ target: 'TARGET_WHATEVER_2',
174
+ }
175
+
176
+ const pseudoError = await create('my_project', cmdObjStub).catch(
177
+ (e) => e
178
+ )
179
+ expect(pseudoError).toBe('success')
180
+ })
181
+
182
+ // restore
183
+ afterEach(() => {
184
+ inquirer.prompt = backup
185
+ })
186
+
187
+ afterAll(async () => {
188
+ await nock.cleanAll()
189
+ })
190
+ })
191
+
192
+ describe('--template', () => {
193
+
194
+ let projectName
195
+ let wookCoffee
196
+ let target
197
+ class WookCoffeStub extends WookCoffee {
198
+ // eslint-disable-next-line no-unused-vars
199
+ verifyFolder(_projectName, _options) {
200
+ return new Promise(resolve => resolve('/tmp'))
201
+ }
202
+
203
+ // eslint-disable-next-line no-unused-vars
204
+ create(_projectName, _templateUrl) {
205
+ return new Promise(resolve => resolve({projectPath: '/tmp'}))
206
+ }
207
+ }
208
+
209
+ beforeEach(() => {
210
+ nock.cleanAll()
211
+ target = getTarget()
212
+ jest.spyOn(workspace, 'availableTargets').mockImplementationOnce(new Promise(resolve => resolve([target])))
213
+ projectName = faker.commerce.product() + Date.now()
214
+ wookCoffee = new WookCoffeStub()
215
+ })
216
+
217
+ afterAll(() => {
218
+ nock.cleanAll()
219
+ })
220
+
221
+
222
+ it('should create miniapp with selected template', async () => {
223
+ jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ accepted: 'CALINDRA_MOBILE' })
224
+ getCreateFactory.mockImplementation(() => wookCoffee)
225
+ const factorySpy = jest.spyOn(wookCoffee, 'create')
226
+ const args = {
227
+ target: 'CALINDRA_MOBILE',
228
+ yes: true,
229
+ template: 'serverless'
230
+ }
231
+ await create(projectName, args)
232
+
233
+ expect(factorySpy).toHaveBeenCalledWith(projectName, 'https://github.com/Calindra/eitri-mobile-mini-app-template', target)
234
+ })
235
+
236
+ it.skip('should return error on select invalid template', async () => {
237
+ jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ accepted: 'CALINDRA_MOBILE' })
238
+ getCreateFactory.mockImplementation(() => wookCoffee)
239
+ const consoleSpy = jest.spyOn(console, 'error')
240
+ jest.spyOn(process, 'exit')
241
+ .mockImplementation((number) => {return number})
242
+ const args = {
243
+ target: 'CALINDRA_MOBILE',
244
+ yes: true,
245
+ template: 'inexistent template'
246
+ }
247
+ await create(projectName, args)
248
+
249
+ expect(consoleSpy).toHaveBeenCalledWith('O template [inexistent template] não existe para o target selecionado.')
250
+ })
251
+ })
252
+ })
@@ -0,0 +1,159 @@
1
+ /* eslint-disable no-undef */
2
+ const path = require('path')
3
+ const {workspace} = require('../../src/service/Workspace')
4
+ const { panicCheckEnvList, environmentVariable, buildEnvFile, sendEnvfile } = require('../../src/util/manage-env')
5
+ const config = require('config')
6
+ const CONFIG_WORKSPACE = config.get('workspace')
7
+ const nock = require('nock')
8
+ const ManageEnvService = require('../../src/service/ManageEnvService')
9
+ const manageEnvService = new ManageEnvService()
10
+
11
+ describe('manage-env', () => {
12
+ const MOCK = true
13
+ const blindGuardian = workspace.blindGuardian
14
+
15
+ beforeEach(() => {
16
+ nock.cleanAll()
17
+ })
18
+
19
+ afterEach(() => {
20
+ nock.cleanAll()
21
+ })
22
+
23
+ it('should fail when envList is empty', async () => {
24
+ expect.assertions(2)
25
+ try {
26
+ panicCheckEnvList([])
27
+ } catch (e) {
28
+ expect(e).toBeDefined()
29
+ expect(e.message).toBe('Nenhuma variavel de ambiente foi fornecida.')
30
+ }
31
+ })
32
+
33
+ it('should fail with wrong key', async () => {
34
+ expect.assertions(2)
35
+ try {
36
+ const envList = ['0FOO=bar']
37
+ environmentVariable(envList)
38
+ } catch (e) {
39
+ expect(e).toBeDefined()
40
+ expect(e.message).toEqual('A chave 0FOO é inválida')
41
+ }
42
+
43
+ })
44
+
45
+ it('should return a object', async () => {
46
+ const envList = ['FOO=bar', 'DEV=back-end']
47
+ const envVariable = environmentVariable(envList)
48
+ expect(envVariable).toEqual({ FOO: 'bar', DEV: 'back-end' })
49
+ })
50
+
51
+ it('should fail if there is no fileNames', () => {
52
+ expect.assertions(2)
53
+ try {
54
+ buildEnvFile('dev', [], { FOO: 'bar', DEV: 'back-end' }, 'miniapp-foo', '0.1.0')
55
+ } catch (e) {
56
+ expect(e).toBeDefined()
57
+ expect(e.message).toEqual('Não existe aquivo na pasta server.')
58
+ }
59
+ })
60
+
61
+ it('should send envFile to /colossus-api/v1', async () => {
62
+ let bodySentToServer, bodySentToCreateLambda
63
+ if (MOCK) {
64
+ nock(CONFIG_WORKSPACE.colossus.url, { allowUnmocked: true })
65
+ .post('/env', body => {
66
+ bodySentToServer = body
67
+ return true
68
+ })
69
+ .reply(200, { ok: true })
70
+ nock('https://dev.eitri.calindra.com.br', { allowUnmocked: true })
71
+ .post('/workspace/server', body => {
72
+ bodySentToCreateLambda = body
73
+ return true
74
+ })
75
+ .reply(200, { ok: true })
76
+ }
77
+ const folder2watch = path.resolve(__dirname, '..', '_fixtures', 'src')
78
+ workspace.setFolder2Watch(folder2watch)
79
+ blindGuardian.readConf()
80
+
81
+ workspace.setServerUrl(CONFIG_WORKSPACE.url)
82
+ await workspace.init()
83
+ try {
84
+ await workspace.postLambda(path.resolve(folder2watch, 'server', 'foo.js'), '/server/foo.js')
85
+ } catch (e) {
86
+ console.error(e)
87
+ throw e
88
+ }
89
+ let miniConf = workspace.getMiniConf()
90
+ const fileNames = await workspace.getFileNames('server')
91
+
92
+ const envFile = {
93
+ envCode: 'ws',
94
+ fileNames,
95
+ envVars: { FOO: 'bar', DEV: 'back-end' },
96
+ slug: miniConf.slug,
97
+ miniAppVersion: miniConf.version
98
+ }
99
+
100
+ const url = `${CONFIG_WORKSPACE.colossus.url}`
101
+ const resp = await sendEnvfile(workspace, url, envFile).catch(e => e)
102
+ expect(resp.constructor.name).not.toBe('Error')
103
+ expect(resp.data).toMatchObject({ ok: true })
104
+ if (MOCK) {
105
+ expect(bodySentToServer).toMatchObject({
106
+ envCode: 'ws',
107
+ fileNames,
108
+ envVars: { FOO: 'bar', DEV: 'back-end' },
109
+ slug: miniConf.slug,
110
+ miniAppVersion: miniConf.version
111
+ })
112
+ expect(bodySentToCreateLambda).toMatchObject({
113
+ 'envCode': 'ws',
114
+ 'miniAppVersion': '0.1.0',
115
+ 'name': 'foo.js',
116
+ 'publishing': false,
117
+ 'slug': 'mini-app-colossus',
118
+ })
119
+ }
120
+ }, 60000)
121
+
122
+ it('should find environment variables', async () => {
123
+ const url = `${CONFIG_WORKSPACE.colossus.url}`
124
+ const query = {
125
+ env: 'ws',
126
+ slug: 'mini-app-colossus',
127
+ miniAppVersion: '0.1.0',
128
+ fileName: 'backend.js',
129
+ email: 'fabio.oshiro@gmail'
130
+ }
131
+ console.log('rota', CONFIG_WORKSPACE.colossus.url)
132
+ // https://dev.eitri.calindra.com.br/workspace/server?env=ws&slug=mini-app-colossus&miniAppVersion=0.1.0&fileName=backend.js&user=fabio.oshiro@gmail
133
+ nock('https://dev.eitri.calindra.com.br/workspace/server', { allowUnmocked: false })
134
+ .get('/env')
135
+ .query(query)
136
+ .reply(200, { FOO: 'BAR', BAR: 'FOO' })
137
+
138
+ const folder2watch = path.resolve(__dirname, '..', '_fixtures', 'src')
139
+ workspace.setFolder2Watch(folder2watch)
140
+ blindGuardian.readConf()
141
+
142
+ workspace.setServerUrl(CONFIG_WORKSPACE.url)
143
+ await workspace.init()
144
+ // await miniLog.connect(workspace.userEmail)
145
+ let miniConf = workspace.getMiniConf()
146
+ let envVariables
147
+
148
+ try {
149
+ envVariables = await manageEnvService.getEnvVariables(workspace, url, miniConf, 'ws', 'fabio.oshiro@gmail', 'backend.js')
150
+ } catch (e) {
151
+ console.error(e)
152
+ throw e
153
+ }
154
+
155
+ console.log('envVariables', envVariables)
156
+
157
+ expect(envVariables).toMatchObject({ FOO: 'BAR', BAR: 'FOO' })
158
+ })
159
+ })
@@ -0,0 +1,74 @@
1
+ /* eslint-disable no-undef */
2
+ // eslint-disable-next-line no-unused-vars
3
+ const inquirer = require('inquirer')
4
+ const process = require('process')
5
+ const list = require('../../src/cmd/list')
6
+ const { workspace } = require('../../src/service/Workspace')
7
+ const { getTarget } = require('../_fixtures/factory')
8
+ const nock = require('nock')
9
+
10
+ jest.mock('inquirer', () => ({
11
+ prompt: jest.fn(() => {
12
+ return new Promise(resolve => resolve({selected: 'AME_DIGITAL_MOBILE'}))
13
+ })
14
+ }))
15
+
16
+ describe.skip('List command test', () => {
17
+ let fakeTarget
18
+ beforeEach(() => {
19
+ nock.cleanAll()
20
+ workspace.setServerUrl('https://dev.eitri.calindra.com.br')
21
+ fakeTarget = getTarget()
22
+ })
23
+ it('should print all miniapp templates of selected target', async () => {
24
+ const args = {
25
+ template: true
26
+ }
27
+
28
+ jest.spyOn(workspace, 'availableTargets').mockResolvedValueOnce([fakeTarget])
29
+ const consoleSpy = jest.spyOn(console, 'log')
30
+ await list(args)
31
+ expect(consoleSpy).toHaveBeenCalledTimes(1)
32
+ expect(consoleSpy).toHaveBeenCalledWith('serverless: Para uso de miniapp com serverless \n')
33
+ })
34
+
35
+ it('should return error if target dont have templates', async () => {
36
+ const args = {
37
+ template: true
38
+ }
39
+ fakeTarget.miniAppBoilerplateList = null
40
+
41
+ jest.spyOn(process, 'exit')
42
+ .mockImplementation((number) => {return number})
43
+
44
+ jest.spyOn(workspace, 'availableTargets').mockResolvedValueOnce([fakeTarget])
45
+
46
+ const consoleSpy = jest.spyOn(console, 'error')
47
+ await list(args)
48
+ expect(consoleSpy).toHaveBeenCalledTimes(1)
49
+ expect(consoleSpy).toHaveBeenCalledWith('O Target AME_DIGITAL_MOBILE não possui boilerplates disponíveis.')
50
+ consoleSpy.mockClear()
51
+ })
52
+
53
+ it('should return error if doesnt have target', async () => {
54
+ const args = {
55
+ template: true
56
+ }
57
+
58
+ nock('https://dev.eitri.calindra.com.br', { allowUnmocked: true })
59
+ .get('/workspace/targets')
60
+ .reply(400, {message: 'xpto'})
61
+
62
+ const consoleSpy = jest.spyOn(console, 'error')
63
+
64
+ await list(args)
65
+
66
+ jest.spyOn(process, 'exit')
67
+ .mockImplementation((number) => {return number})
68
+
69
+ expect(consoleSpy).toHaveBeenCalledTimes(1)
70
+ expect(consoleSpy).toHaveBeenLastCalledWith('Houve um erro ao listar os targets.')
71
+
72
+ })
73
+
74
+ })
@@ -0,0 +1,168 @@
1
+ /* eslint-disable no-undef */
2
+ const path = require('path')
3
+ const {workspace} = require('../../src/service/Workspace')
4
+ const { panicCheckEnvList, environmentVariable, buildEnvFile, sendEnvfile } = require('../../src/util/manage-env')
5
+ const config = require('config')
6
+ const CONFIG_WORKSPACE = config.get('workspace')
7
+ const nock = require('nock')
8
+ const ManageEnvService = require('../../src/service/ManageEnvService')
9
+ const manageEnvService = new ManageEnvService()
10
+
11
+ describe('manage-env', () => {
12
+ const MOCK = true
13
+ const blindGuardian = workspace.blindGuardian
14
+
15
+ beforeEach(() => {
16
+ nock.cleanAll()
17
+ })
18
+
19
+ afterEach(() => {
20
+ nock.cleanAll()
21
+ })
22
+
23
+ it('should fail when envList is empty', async () => {
24
+ expect.assertions(2)
25
+ try {
26
+ panicCheckEnvList([])
27
+ } catch (e) {
28
+ expect(e).toBeDefined()
29
+ expect(e.message).toBe('Nenhuma variavel de ambiente foi fornecida.')
30
+ }
31
+ })
32
+
33
+ it('should fail with wrong key', async () => {
34
+ expect.assertions(2)
35
+ try {
36
+ const envList = ['0FOO=bar']
37
+ environmentVariable(envList)
38
+ } catch (e) {
39
+ expect(e).toBeDefined()
40
+ expect(e.message).toEqual('A chave 0FOO é inválida')
41
+ }
42
+
43
+ })
44
+
45
+ it('should return a object', async () => {
46
+ const envList = ['FOO=bar', 'DEV=back-end']
47
+ const envVariable = environmentVariable(envList)
48
+ expect(envVariable).toEqual({ FOO: 'bar', DEV: 'back-end' })
49
+ })
50
+
51
+ it('should fail if there is no fileNames', () => {
52
+ expect.assertions(2)
53
+ try {
54
+ buildEnvFile('dev', [], { FOO: 'bar', DEV: 'back-end' }, 'miniapp-foo', '0.1.0')
55
+ } catch (e) {
56
+ expect(e).toBeDefined()
57
+ expect(e.message).toEqual('Não existe aquivo na pasta server.')
58
+ }
59
+ })
60
+
61
+ it('should send envFile to /colossus-api/v1', async () => {
62
+ let bodySentToServer, bodySentToCreateLambda
63
+ if (MOCK) {
64
+
65
+ nock('https://dev.eitri.calindra.com.br')
66
+ .post('/blind-guardian-api/v1/o/auth')
67
+ .reply(200, { accessToken: 'xpto' })
68
+
69
+ nock(CONFIG_WORKSPACE.colossus.url, { allowUnmocked: true })
70
+ .post('/env', body => {
71
+ bodySentToServer = body
72
+ return true
73
+ })
74
+ .reply(200, { ok: true })
75
+ nock('https://dev.eitri.calindra.com.br', { allowUnmocked: true })
76
+ .post('/workspace/server', body => {
77
+ bodySentToCreateLambda = body
78
+ return true
79
+ })
80
+ .reply(200, { ok: true })
81
+ }
82
+ const folder2watch = path.resolve(__dirname, '..', '_fixtures', 'src')
83
+ workspace.setFolder2Watch(folder2watch)
84
+ blindGuardian.readConf()
85
+
86
+ workspace.setServerUrl(CONFIG_WORKSPACE.url)
87
+ await workspace.init()
88
+ try {
89
+ await workspace.postLambda(path.resolve(folder2watch, 'server', 'foo.js'), '/server/foo.js')
90
+ } catch (e) {
91
+ console.error(e)
92
+ throw e
93
+ }
94
+ let miniConf = workspace.getMiniConf()
95
+ const fileNames = await workspace.getFileNames('server')
96
+
97
+ const envFile = {
98
+ envCode: 'ws',
99
+ fileNames,
100
+ envVars: { FOO: 'bar', DEV: 'back-end' },
101
+ slug: miniConf.slug,
102
+ miniAppVersion: miniConf.version
103
+ }
104
+
105
+ const url = `${CONFIG_WORKSPACE.colossus.url}`
106
+ const resp = await sendEnvfile(workspace, url, envFile).catch(e => e)
107
+ expect(resp.constructor.name).not.toBe('Error')
108
+ expect(resp.data).toMatchObject({ ok: true })
109
+ if (MOCK) {
110
+ expect(bodySentToServer).toMatchObject({
111
+ envCode: 'ws',
112
+ fileNames,
113
+ envVars: { FOO: 'bar', DEV: 'back-end' },
114
+ slug: miniConf.slug,
115
+ miniAppVersion: miniConf.version
116
+ })
117
+ expect(bodySentToCreateLambda).toMatchObject({
118
+ 'envCode': 'ws',
119
+ 'miniAppVersion': '0.1.0',
120
+ 'name': 'foo.js',
121
+ 'publishing': false,
122
+ 'slug': 'mini-app-colossus',
123
+ })
124
+ }
125
+ }, 60000)
126
+
127
+ it('should find environment variables', async () => {
128
+ const url = `${CONFIG_WORKSPACE.colossus.url}`
129
+ const query = {
130
+ env: 'ws',
131
+ slug: 'mini-app-colossus',
132
+ miniAppVersion: '0.1.0',
133
+ fileName: 'backend.js',
134
+ email: 'fabio.oshiro@gmail'
135
+ }
136
+ console.log('rota', CONFIG_WORKSPACE.colossus.url)
137
+
138
+ nock('https://dev.eitri.calindra.com.br')
139
+ .post('/blind-guardian-api/v1/o/auth')
140
+ .reply(200, { accessToken: 'xpto' })
141
+
142
+ nock('https://dev.eitri.calindra.com.br/workspace/server', { allowUnmocked: false })
143
+ .get('/env')
144
+ .query(query)
145
+ .reply(200, { FOO: 'BAR', BAR: 'FOO' })
146
+
147
+ const folder2watch = path.resolve(__dirname, '..', '_fixtures', 'src')
148
+ workspace.setFolder2Watch(folder2watch)
149
+ blindGuardian.readConf()
150
+
151
+ workspace.setServerUrl(CONFIG_WORKSPACE.url)
152
+ await workspace.init()
153
+ // await miniLog.connect(workspace.userEmail)
154
+ let miniConf = workspace.getMiniConf()
155
+ let envVariables
156
+
157
+ try {
158
+ envVariables = await manageEnvService.getEnvVariables(workspace, url, miniConf, 'ws', 'fabio.oshiro@gmail', 'backend.js')
159
+ } catch (e) {
160
+ console.error(e)
161
+ throw e
162
+ }
163
+
164
+ console.log('envVariables', envVariables)
165
+
166
+ expect(envVariables).toMatchObject({ FOO: 'BAR', BAR: 'FOO' })
167
+ })
168
+ })
@@ -0,0 +1,20 @@
1
+ /* eslint-disable no-undef */
2
+
3
+ const signup = require('../../src/cmd/signup')
4
+ const nock = require('nock')
5
+
6
+ jest.setTimeout(15000)
7
+
8
+
9
+ describe('signup', () => {
10
+
11
+ it('Should succesfully create an user', () => {
12
+
13
+ nock('https://dev.eitri.calindra.com.br')
14
+ .post('/blind-guardian-api/v1/user')
15
+ .reply(200, { id: 'xpto', scopes: [ 'foo', 'bar' ] })
16
+
17
+
18
+ })
19
+
20
+ })
@@ -0,0 +1,5 @@
1
+ /* eslint-disable no-undef */
2
+
3
+ describe('start', () => {
4
+ it('sincroniza os arquivos com o servidor', () => {})
5
+ })
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ "version": "0.1.0"
3
+ }