|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {expect} from 'chai'; |
| 8 | +import {execa} from 'execa'; |
| 9 | +import path from 'node:path'; |
| 10 | +import {fileURLToPath} from 'node:url'; |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = path.dirname(__filename); |
| 14 | + |
| 15 | +/** |
| 16 | + * E2E Tests for Authentication Token Generation |
| 17 | + */ |
| 18 | +describe('Auth Token E2E Tests', function () { |
| 19 | + this.timeout(120_000); // 2 minutes |
| 20 | + this.retries(2); |
| 21 | + |
| 22 | + const CLI_BIN = path.resolve(__dirname, '../../../bin/run.js'); |
| 23 | + |
| 24 | + before(function () { |
| 25 | + if (!process.env.SFCC_CLIENT_ID || !process.env.SFCC_CLIENT_SECRET) { |
| 26 | + this.skip(); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + async function runCLI(args: string[], env?: Record<string, string>) { |
| 31 | + const result = await execa('node', [CLI_BIN, ...args], { |
| 32 | + env: { |
| 33 | + ...process.env, |
| 34 | + ...env, |
| 35 | + SFCC_LOG_LEVEL: 'silent', |
| 36 | + }, |
| 37 | + reject: false, |
| 38 | + }); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + function decodeJWT(token: string): Record<string, unknown> { |
| 43 | + const parts = token.split('.'); |
| 44 | + if (parts.length !== 3) { |
| 45 | + throw new Error('Invalid JWT format'); |
| 46 | + } |
| 47 | + const payload = Buffer.from(parts[1], 'base64').toString('utf8'); |
| 48 | + return JSON.parse(payload); |
| 49 | + } |
| 50 | + |
| 51 | + it('should generate a valid OAuth token with correct format, scopes, and expiration', async function () { |
| 52 | + const result = await runCLI(['auth:token', '--json']); |
| 53 | + expect(result.exitCode).to.equal(0, `Token generation failed: ${result.stderr}`); |
| 54 | + expect(result.stdout).to.not.be.empty; |
| 55 | + |
| 56 | + const response = JSON.parse(result.stdout); |
| 57 | + expect(response).to.be.an('object'); |
| 58 | + expect(response.accessToken).to.be.a('string').and.not.be.empty; |
| 59 | + expect(response.expires).to.be.a('string'); |
| 60 | + expect(response.scopes).to.be.an('array').that.is.not.empty; |
| 61 | + |
| 62 | + // Validate JWT format |
| 63 | + const payload = decodeJWT(response.accessToken); |
| 64 | + expect(payload.sub).to.exist; |
| 65 | + expect(payload.exp).to.exist; |
| 66 | + |
| 67 | + // Validate expiration |
| 68 | + const now = Math.floor(Date.now() / 1000); |
| 69 | + expect(payload.exp as number).to.be.greaterThan(now); |
| 70 | + expect((payload.exp as number) - now).to.be.lessThan(86_400); |
| 71 | + |
| 72 | + // Validate expires field matches exp approximately |
| 73 | + const expiresDate = new Date(response.expires).getTime() / 1000; |
| 74 | + expect(Math.abs(expiresDate - (payload.exp as number))).to.be.lessThan(10); |
| 75 | + |
| 76 | + // Validate scopes |
| 77 | + expect(payload.scope, 'Token should contain scope claim').to.exist; |
| 78 | + const tokenScopes = Array.isArray(payload.scope) ? payload.scope : (payload.scope as string).split(' '); |
| 79 | + for (const s of response.scopes as string[]) { |
| 80 | + expect(tokenScopes, `Token should include scope "${s}"`).to.include(s); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + describe('Generate Token With Additional Scopes', function () { |
| 85 | + it('should generate a token with allowed additional scopes', async function () { |
| 86 | + // Use only scopes your client actually has |
| 87 | + const extraScopes = ['profile', 'roles']; |
| 88 | + |
| 89 | + const result = await runCLI(['auth:token', '--scope', extraScopes.join(','), '--json']); |
| 90 | + |
| 91 | + expect(result.exitCode).to.equal(0, `Token generation with extra scopes failed: ${result.stderr}`); |
| 92 | + |
| 93 | + const response = JSON.parse(result.stdout); |
| 94 | + const accessToken = response.accessToken as string; |
| 95 | + expect(accessToken).to.be.a('string').and.not.be.empty; |
| 96 | + expect(response.scopes).to.include.members(extraScopes); |
| 97 | + |
| 98 | + const payload = decodeJWT(accessToken); |
| 99 | + expect(payload.scope).to.exist; |
| 100 | + |
| 101 | + const tokenScopes = Array.isArray(payload.scope) ? payload.scope : (payload.scope as string).split(' '); |
| 102 | + |
| 103 | + for (const s of extraScopes) { |
| 104 | + expect(tokenScopes, `Token should include scope "${s}"`).to.include(s); |
| 105 | + } |
| 106 | + |
| 107 | + console.log(`Token with additional scopes: ${tokenScopes.join(', ')}`); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('Invalid Credentials', function () { |
| 112 | + it('should fail with invalid client credentials', async function () { |
| 113 | + const result = await runCLI(['auth:token', '--json'], { |
| 114 | + SFCC_CLIENT_ID: 'invalid-client-id', |
| 115 | + SFCC_CLIENT_SECRET: 'invalid-client-secret', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(result.exitCode).to.not.equal(0); |
| 119 | + expect(result.stderr).to.not.be.empty; |
| 120 | + expect(result.stderr).to.match(/401|unauthorized|invalid.*client/i); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('JSON Output Structure', function () { |
| 125 | + it('should return correct JSON keys', async function () { |
| 126 | + const result = await runCLI(['auth:token', '--json']); |
| 127 | + const response = JSON.parse(result.stdout); |
| 128 | + expect(response).to.have.all.keys('accessToken', 'expires', 'scopes'); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('Default Scopes', function () { |
| 133 | + it('should return default scopes when no scopes are requested', async function () { |
| 134 | + const result = await runCLI(['auth:token', '--json']); |
| 135 | + const response = JSON.parse(result.stdout); |
| 136 | + expect(response.scopes.length).to.be.greaterThan(0); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('Non-JSON Output', function () { |
| 141 | + it('should output raw token in non-JSON mode', async function () { |
| 142 | + const result = await runCLI(['auth:token']); |
| 143 | + expect(result.exitCode).to.equal(0); |
| 144 | + expect(result.stdout).to.match(/^ey[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/); // JWT regex |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments