-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcreateOrganization.test.ts
More file actions
133 lines (107 loc) · 4.62 KB
/
createOrganization.test.ts
File metadata and controls
133 lines (107 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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 {AsgardeoAPIError, Organization, CreateOrganizationPayload} from '@asgardeo/node';
import {describe, it, expect, vi, beforeEach, afterEach, Mock} from 'vitest';
// Adjust these paths if your project structure is different
import AsgardeoNextClient from '../../../AsgardeoNextClient';
import createOrganization from '../createOrganization';
// Use the same class so we can assert instanceof and status code propagation
// Pull the mocked modules so we can access their spies
import getSessionId from '../getSessionId';
// ---- Mocks ----
vi.mock('../../../AsgardeoNextClient', () =>
// We return a default export with a static getInstance function we can stub
({
default: {
getInstance: vi.fn(),
},
}),
);
vi.mock('../getSessionId', () => ({
default: vi.fn(),
}));
describe('createOrganization (Next.js server action)', () => {
const mockClient: {createOrganization: ReturnType<typeof vi.fn>} = {
createOrganization: vi.fn(),
};
const basePayload: CreateOrganizationPayload = {
description: 'Screen sharing organization',
name: 'Team Viewer',
orgHandle: 'team-viewer',
parentId: 'parent-123',
type: 'TENANT',
};
const mockOrg: Organization = {
id: 'org-001',
name: 'Team Viewer',
orgHandle: 'team-viewer',
};
beforeEach(() => {
vi.resetAllMocks();
// Default: getInstance returns our mock client
(AsgardeoNextClient.getInstance as unknown as Mock).mockReturnValue(mockClient);
// Default: getSessionId resolves to a session id
(getSessionId as unknown as Mock).mockResolvedValue('sess-abc');
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should create an organization successfully when a sessionId is provided', async () => {
mockClient.createOrganization.mockResolvedValueOnce(mockOrg);
const result: Organization = await createOrganization(0, basePayload, 'sess-123');
expect(AsgardeoNextClient.getInstance).toHaveBeenCalledTimes(1);
expect(getSessionId).not.toHaveBeenCalled();
expect(mockClient.createOrganization).toHaveBeenCalledWith(basePayload, 'sess-123');
expect(result).toEqual(mockOrg);
});
it('should fall back to getSessionId when sessionId is undefined', async () => {
mockClient.createOrganization.mockResolvedValueOnce(mockOrg);
const result: Organization = await createOrganization(0, basePayload, undefined as unknown as string);
expect(getSessionId).toHaveBeenCalledTimes(1);
expect(mockClient.createOrganization).toHaveBeenCalledWith(basePayload, 'sess-abc');
expect(result).toEqual(mockOrg);
});
it('should fall back to getSessionId when sessionId is null', async () => {
mockClient.createOrganization.mockResolvedValueOnce(mockOrg);
const result: Organization = await createOrganization(0, basePayload, null as unknown as string);
expect(getSessionId).toHaveBeenCalledTimes(1);
expect(mockClient.createOrganization).toHaveBeenCalledWith(basePayload, 'sess-abc');
expect(result).toEqual(mockOrg);
});
it('should not call getSessionId when an empty string is passed (empty string is not nullish)', async () => {
mockClient.createOrganization.mockResolvedValueOnce(mockOrg);
const result: Organization = await createOrganization(0, basePayload, '');
expect(getSessionId).not.toHaveBeenCalled();
expect(mockClient.createOrganization).toHaveBeenCalledWith(basePayload, '');
expect(result).toEqual(mockOrg);
});
it('should wrap an AsgardeoAPIError thrown by client.createOrganization, preserving statusCode', async () => {
const original: AsgardeoAPIError = new AsgardeoAPIError(
'Upstream validation failed',
'ORG_CREATE_400',
'server',
400,
);
mockClient.createOrganization.mockRejectedValueOnce(original);
await expect(createOrganization(0, basePayload, 'sess-1')).rejects.toMatchObject({
constructor: AsgardeoAPIError,
message: expect.stringContaining('Failed to create the organization: Upstream validation failed'),
statusCode: 400,
});
});
});