auth/tests/utils.test.ts
2023-11-15 08:33:15 -05:00

69 lines
2.1 KiB
TypeScript

// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { buildDomainWideDelegationJWT, generateCredentialsFilename } from '../src/utils';
describe('Utils', () => {
describe('#buildDomainWideDelegationJWT', () => {
const cases = [
{
name: 'default',
serviceAccount: 'my-service@example.com',
lifetime: 1000,
},
{
name: 'with subject',
serviceAccount: 'my-service@example.com',
subject: 'my-subject',
lifetime: 1000,
},
{
name: 'with scopes',
serviceAccount: 'my-service@example.com',
scopes: ['scope1', 'scope2'],
lifetime: 1000,
},
];
cases.forEach((tc) => {
it(tc.name, async () => {
const val = buildDomainWideDelegationJWT(
tc.serviceAccount,
tc.subject,
tc.scopes,
tc.lifetime,
);
const body = JSON.parse(val);
assert.deepStrictEqual(body.iss, tc.serviceAccount);
assert.deepStrictEqual(body.aud, 'https://oauth2.googleapis.com/token');
assert.deepStrictEqual(body.sub, tc.subject);
assert.deepStrictEqual(body.scope, tc.scopes?.join(' '));
});
});
});
describe('#generateCredentialsFilename', () => {
it('returns a string matching the regex', () => {
for (let i = 0; i < 10; i++) {
const filename = generateCredentialsFilename();
assert.match(filename, /gha-creds-[0-9a-z]{16}\.json/);
}
});
});
});