Skip to content

Commit b5e8e09

Browse files
committed
test infra wip
1 parent 06a9aa8 commit b5e8e09

12 files changed

Lines changed: 313 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/dist
66
/tmp
77
/node_modules
8+
/coverage
89
oclif.manifest.json
910

1011

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"scripts": {
77
"start": "pnpm --filter @salesforce/b2c-cli run dev",
88
"test": "pnpm -r test",
9+
"test:unit": "pnpm -r run test:unit",
10+
"coverage": "pnpm -r run coverage",
911
"format": "pnpm -r run format",
1012
"lint": "pnpm -r run lint",
1113
"build": "pnpm -r run build",

packages/b2c-cli/.c8rc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"all": true,
3+
"src": ["src"],
4+
"exclude": [
5+
"test/**",
6+
"**/*.d.ts"
7+
],
8+
"reporter": ["text", "html", "lcov"],
9+
"report-dir": "coverage"
10+
}

packages/b2c-cli/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/build
77
/tmp
88
/node_modules
9+
/coverage
910
oclif.manifest.json
1011

1112

packages/b2c-cli/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@types/chai": "^4",
2727
"@types/mocha": "^10",
2828
"@types/node": "^18",
29+
"c8": "^10.1.3",
2930
"chai": "^4",
3031
"eslint": "^9",
3132
"eslint-config-oclif": "^6",
@@ -116,7 +117,9 @@
116117
"posttest": "pnpm run lint",
117118
"prepack": "oclif manifest && oclif readme",
118119
"pretest": "tsc --noEmit -p test",
119-
"test": "OCLIF_TEST_ROOT=. mocha --forbid-only \"test/**/*.test.ts\"",
120+
"test": "c8 env OCLIF_TEST_ROOT=. mocha --forbid-only \"test/**/*.test.ts\"",
121+
"test:unit": "env OCLIF_TEST_ROOT=. mocha --forbid-only \"test/**/*.test.ts\"",
122+
"coverage": "c8 report",
120123
"version": "oclif readme && git add README.md",
121124
"dev": "node ./bin/dev.js"
122125
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"all": true,
3+
"src": ["src"],
4+
"exclude": [
5+
"src/clients/*.generated.ts",
6+
"test/**",
7+
"**/*.d.ts"
8+
],
9+
"reporter": ["text", "html", "lcov"],
10+
"report-dir": "coverage"
11+
}

packages/b2c-tooling-sdk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/dist
66
/tmp
77
/node_modules
8+
/coverage
89

910

1011
yarn.lock
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"node-option": ["import=tsx"],
3+
"timeout": 10000,
4+
"recursive": true,
5+
"extension": ["ts"]
6+
}

packages/b2c-tooling-sdk/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@
162162
"lint": "eslint",
163163
"format": "prettier --write src",
164164
"format:check": "prettier --check src",
165-
"test": "echo \"Error: no test specified\" && exit 0",
165+
"pretest": "tsc --noEmit -p test",
166+
"test": "c8 mocha --forbid-only \"test/**/*.test.ts\"",
167+
"test:unit": "mocha --forbid-only \"test/**/*.test.ts\"",
168+
"coverage": "c8 report",
166169
"posttest": "pnpm run lint"
167170
},
168171
"devDependencies": {
@@ -172,14 +175,20 @@
172175
"@salesforce/dev-config": "^4.3.2",
173176
"@tony.ganchev/eslint-plugin-header": "^3.1.11",
174177
"@types/archiver": "^7.0.0",
178+
"@types/chai": "^4.3.20",
179+
"@types/mocha": "^10.0.10",
175180
"@types/node": "^18.19.130",
176181
"@types/xml2js": "^0.4.14",
182+
"c8": "^10.1.3",
183+
"chai": "^4.5.0",
177184
"eslint": "^9",
178185
"eslint-config-prettier": "^10",
179186
"eslint-plugin-prettier": "^5.5.4",
187+
"mocha": "^10.8.2",
180188
"openapi-typescript": "^7.10.1",
181189
"prettier": "^3.6.2",
182190
"shx": "^0.3.3",
191+
"tsx": "^4.20.6",
183192
"typescript": "^5",
184193
"typescript-eslint": "^8"
185194
},
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
import {expect} from 'chai';
7+
import {checkAvailableAuthMethods} from '../../src/auth/resolve.js';
8+
9+
describe('auth/resolve', () => {
10+
describe('checkAvailableAuthMethods', () => {
11+
it('returns client-credentials when clientId and clientSecret are provided', () => {
12+
const result = checkAvailableAuthMethods({
13+
clientId: 'test-client',
14+
clientSecret: 'test-secret',
15+
});
16+
17+
expect(result.available).to.include('client-credentials');
18+
});
19+
20+
it('returns implicit when only clientId is provided', () => {
21+
const result = checkAvailableAuthMethods({
22+
clientId: 'test-client',
23+
});
24+
25+
expect(result.available).to.include('implicit');
26+
expect(result.available).to.not.include('client-credentials');
27+
});
28+
29+
it('returns basic when username and password are provided', () => {
30+
const result = checkAvailableAuthMethods({
31+
username: 'test-user',
32+
password: 'test-pass',
33+
});
34+
35+
expect(result.available).to.include('basic');
36+
});
37+
38+
it('returns api-key when apiKey is provided', () => {
39+
const result = checkAvailableAuthMethods({
40+
apiKey: 'test-api-key',
41+
});
42+
43+
expect(result.available).to.include('api-key');
44+
});
45+
46+
it('returns unavailable with reason when clientSecret is missing for client-credentials', () => {
47+
const result = checkAvailableAuthMethods({clientId: 'test-client'}, ['client-credentials']);
48+
49+
expect(result.available).to.have.length(0);
50+
expect(result.unavailable).to.have.length(1);
51+
expect(result.unavailable[0]).to.deep.equal({
52+
method: 'client-credentials',
53+
reason: 'clientSecret is required',
54+
});
55+
});
56+
57+
it('returns unavailable with reason when clientId is missing', () => {
58+
const result = checkAvailableAuthMethods({}, ['client-credentials', 'implicit']);
59+
60+
expect(result.unavailable).to.have.length(2);
61+
expect(result.unavailable[0].reason).to.equal('clientId is required');
62+
expect(result.unavailable[1].reason).to.equal('clientId is required');
63+
});
64+
65+
it('only checks allowed methods', () => {
66+
const result = checkAvailableAuthMethods(
67+
{
68+
clientId: 'test-client',
69+
clientSecret: 'test-secret',
70+
username: 'test-user',
71+
password: 'test-pass',
72+
},
73+
['basic'],
74+
);
75+
76+
expect(result.available).to.deep.equal(['basic']);
77+
expect(result.unavailable).to.have.length(0);
78+
});
79+
80+
it('returns all available methods when credentials support multiple', () => {
81+
const result = checkAvailableAuthMethods({
82+
clientId: 'test-client',
83+
clientSecret: 'test-secret',
84+
username: 'test-user',
85+
password: 'test-pass',
86+
apiKey: 'test-key',
87+
});
88+
89+
expect(result.available).to.have.length(4);
90+
expect(result.available).to.include('client-credentials');
91+
expect(result.available).to.include('implicit');
92+
expect(result.available).to.include('basic');
93+
expect(result.available).to.include('api-key');
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)