Skip to content

Commit ad24276

Browse files
committed
Add tests for case-sensitive environment variables
Signed-off-by: Kyle Harding <[email protected]>
1 parent c1bc922 commit ad24276

File tree

1 file changed

+142
-2
lines changed

1 file changed

+142
-2
lines changed

test/unit/lib/plugins/environments.test.js

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ describe('Environments Plugin test suite', () => {
365365
name: environment_name,
366366
variables: [
367367
{
368-
name: 'test',
368+
name: 'TEST',
369369
value: 'test'
370370
}
371371
]
@@ -392,11 +392,151 @@ describe('Environments Plugin test suite', () => {
392392
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments', { org, repo });
393393
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name });
394394
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { org, repo, environment_name });
395+
// https://docs.github.com/en/rest/actions/variables#create-an-environment-variable
396+
// Body parameters name and value are case-sensitive!
395397
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/variables', expect.objectContaining({
396398
org,
397399
repo,
398400
environment_name: environment_name,
399-
name: 'test',
401+
name: 'TEST',
402+
value: 'test'
403+
}));
404+
})
405+
})
406+
})
407+
408+
// patch variable
409+
describe('When there are existing variables and one requires an update', () => {
410+
it('detect divergence and add the variable', async () => {
411+
//arrange
412+
environment_name = 'variables_environment'
413+
// represent config with a reviewers being a user and a team
414+
const plugin = new Environments(undefined, github, { owner: org, repo }, [
415+
{
416+
name: environment_name,
417+
variables: [
418+
{
419+
name: 'test',
420+
value: 'test'
421+
}
422+
]
423+
}
424+
], log, errors);
425+
426+
//model an existing environment with no reviewers
427+
when(github.request)
428+
.calledWith('GET /repos/:org/:repo/environments', { org, repo })
429+
.mockResolvedValue({
430+
data: {
431+
environments: [
432+
fillEnvironment({
433+
name: environment_name,
434+
variables: []
435+
})
436+
]
437+
}
438+
});
439+
440+
when(github.request)
441+
.calledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name })
442+
.mockResolvedValue({
443+
data: {
444+
variables: [
445+
{
446+
name: 'test',
447+
value: 'old'
448+
},
449+
{
450+
name: 'test2',
451+
value: 'test2'
452+
}
453+
]
454+
}
455+
})
456+
457+
//act - run sync() in environments.js
458+
await plugin.sync().then(() => {
459+
//assert - update the variables
460+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments', { org, repo });
461+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name });
462+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { org, repo, environment_name });
463+
// https://docs.github.com/en/rest/actions/variables#update-an-environment-variable
464+
// URL parameter variable_name is case-insensitive!
465+
expect(github.request).toHaveBeenCalledWith('PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name', expect.objectContaining({
466+
org,
467+
repo,
468+
environment_name: environment_name,
469+
variable_name: expect.stringMatching(/test/i),
470+
value: 'test'
471+
}));
472+
})
473+
})
474+
})
475+
476+
// patch variable
477+
describe('When there are existing mixed-case variables and one requires an update', () => {
478+
it('detect divergence and update the variable', async () => {
479+
//arrange
480+
environment_name = 'variables_environment'
481+
// represent config with a reviewers being a user and a team
482+
const plugin = new Environments(undefined, github, { owner: org, repo }, [
483+
{
484+
name: environment_name,
485+
variables: [
486+
{
487+
name: 'TEST',
488+
value: 'test'
489+
}
490+
]
491+
}
492+
], log, errors);
493+
494+
//model an existing environment with no reviewers
495+
when(github.request)
496+
.calledWith('GET /repos/:org/:repo/environments', { org, repo })
497+
.mockResolvedValue({
498+
data: {
499+
environments: [
500+
fillEnvironment({
501+
name: environment_name,
502+
variables: []
503+
})
504+
]
505+
}
506+
});
507+
508+
when(github.request)
509+
.calledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name })
510+
.mockResolvedValue({
511+
data: {
512+
variables: [
513+
{
514+
name: 'TEST',
515+
value: 'old'
516+
},
517+
{
518+
name: 'TEST2',
519+
value: 'test2'
520+
}
521+
]
522+
}
523+
})
524+
525+
//act - run sync() in environments.js
526+
await plugin.sync().then(() => {
527+
//assert - update the variables
528+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments', { org, repo });
529+
// https://docs.github.com/en/rest/actions/variables#list-environment-variables
530+
// Returned properties are case-sensitive!
531+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name });
532+
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { org, repo, environment_name });
533+
// https://docs.github.com/en/rest/actions/variables#update-an-environment-variable
534+
// URL parameter variable_name is case-insensitive!
535+
expect(github.request).toHaveBeenCalledWith('PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name', expect.objectContaining({
536+
org,
537+
repo,
538+
environment_name: environment_name,
539+
variable_name: expect.stringMatching(/test/i),
400540
value: 'test'
401541
}));
402542
})

0 commit comments

Comments
 (0)