Skip to content

Commit 91248f9

Browse files
rostalancursoragent
andcommitted
test: address PR review comments for orchestrator scaffolder actions
- Remove duplicated getError function - Add test for undefined token response in getRequestConfigOption - Remove redundant isDryRun assignment in runWorkflow.test.ts - Add missing blank lines after license headers - Use 'as any' instead of 'as never' in utils.test.ts - Add changeset Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 1837a35 commit 91248f9

6 files changed

Lines changed: 37 additions & 33 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-scaffolder-backend-module-orchestrator': patch
3+
---
4+
5+
Added unit test coverage for orchestrator scaffolder actions and fixed token fallback logic.

workspaces/orchestrator/plugins/scaffolder-backend-module-orchestrator/src/actions/getWorkflowParams.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
1718
import { JsonObject } from '@backstage/types';
1819

@@ -25,6 +26,7 @@ jest.mock('axios', () => ({
2526
}));
2627

2728
jest.mock('./utils', () => ({
29+
...jest.requireActual('./utils'),
2830
getOrchestratorApi: jest.fn(),
2931
getRequestConfigOption: jest.fn(),
3032
}));

workspaces/orchestrator/plugins/scaffolder-backend-module-orchestrator/src/actions/getWorkflowParams.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,9 @@ import { DiscoveryApi } from '@backstage/plugin-permission-common';
1818
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
1919
import { JsonObject } from '@backstage/types';
2020

21-
import { isAxiosError } from 'axios';
2221
import { dump } from 'js-yaml';
2322

24-
import { getOrchestratorApi, getRequestConfigOption } from './utils';
25-
26-
const getError = (err: unknown): Error => {
27-
if (
28-
isAxiosError<{ error: { message: string; name: string } }>(err) &&
29-
err.response?.data?.error?.message
30-
) {
31-
const error = new Error(err.response?.data?.error?.message);
32-
error.name = err.response?.data?.error?.name || 'Error';
33-
return error;
34-
}
35-
return err as Error;
36-
};
23+
import { getError, getOrchestratorApi, getRequestConfigOption } from './utils';
3724

3825
const indentString = (str: string, indent: number) =>
3926
indent ? str.replace(/^/gm, ' '.repeat(indent)) : str;

workspaces/orchestrator/plugins/scaffolder-backend-module-orchestrator/src/actions/runWorkflow.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
1718

1819
import { createRunWorkflowAction } from './runWorkflow';
@@ -24,6 +25,7 @@ jest.mock('axios', () => ({
2425
}));
2526

2627
jest.mock('./utils', () => ({
28+
...jest.requireActual('./utils'),
2729
getOrchestratorApi: jest.fn(),
2830
getRequestConfigOption: jest.fn(),
2931
}));
@@ -92,7 +94,6 @@ describe('createRunWorkflowAction', () => {
9294
templateInfo: {
9395
entityRef: 'component:default/sample-service',
9496
},
95-
isDryRun: true,
9697
logger: {
9798
...createMockActionContext().logger,
9899
info: loggerInfo,

workspaces/orchestrator/plugins/scaffolder-backend-module-orchestrator/src/actions/runWorkflow.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,7 @@ import { AuthService } from '@backstage/backend-plugin-api';
1717
import { DiscoveryApi } from '@backstage/plugin-permission-common';
1818
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
1919

20-
import { isAxiosError } from 'axios';
21-
22-
import { getOrchestratorApi, getRequestConfigOption } from './utils';
23-
24-
const getError = (err: unknown): Error => {
25-
if (
26-
isAxiosError<{ error: { message: string; name: string } }>(err) &&
27-
err.response?.data?.error?.message
28-
) {
29-
const error = new Error(err.response?.data?.error?.message);
30-
error.name = err.response?.data?.error?.name || 'Error';
31-
return error;
32-
}
33-
return err as Error;
34-
};
20+
import { getError, getOrchestratorApi, getRequestConfigOption } from './utils';
3521

3622
export function createRunWorkflowAction(
3723
discoveryService: DiscoveryApi,

workspaces/orchestrator/plugins/scaffolder-backend-module-orchestrator/src/actions/utils.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import { AuthService } from '@backstage/backend-plugin-api';
1718
import { DiscoveryApi } from '@backstage/plugin-permission-common';
1819

@@ -87,9 +88,9 @@ describe('utils', () => {
8788
const axiosInstance = { request: jest.fn() };
8889
const apiInstance = { executeWorkflow: jest.fn() };
8990

90-
mockedAxios.create.mockReturnValue(axiosInstance as never);
91-
MockedConfiguration.mockImplementation(() => ({}) as never);
92-
MockedDefaultApi.mockImplementation(() => apiInstance as never);
91+
mockedAxios.create.mockReturnValue(axiosInstance as any);
92+
MockedConfiguration.mockImplementation(() => ({}) as any);
93+
MockedDefaultApi.mockImplementation(() => apiInstance as any);
9394

9495
const api = await getOrchestratorApi(discoveryService);
9596

@@ -159,5 +160,27 @@ describe('utils', () => {
159160
},
160161
});
161162
});
163+
164+
it('falls back to the backstage secret when the auth service returns undefined', async () => {
165+
const ctx = {
166+
getInitiatorCredentials: jest.fn().mockResolvedValue({
167+
principal: 'user:default/bob',
168+
}),
169+
secrets: {
170+
backstageToken: 'fallback-token',
171+
},
172+
};
173+
const authService = {
174+
getPluginRequestToken: jest.fn().mockResolvedValue(undefined),
175+
} as unknown as jest.Mocked<AuthService>;
176+
177+
const config = await getRequestConfigOption(authService, ctx);
178+
179+
expect(config).toEqual({
180+
headers: {
181+
Authorization: 'Bearer fallback-token',
182+
},
183+
});
184+
});
162185
});
163186
});

0 commit comments

Comments
 (0)