0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test: add organization invitation api tests

This commit is contained in:
Gao Sun 2024-01-30 09:45:01 +08:00
parent f74fdce9a0
commit 891c207817
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
5 changed files with 51 additions and 2 deletions

View file

@ -14,6 +14,8 @@ import type Queries from '#src/tenants/Queries.js';
import { type ConnectorLibrary } from './connector.js';
const invitationLinkPath = '/invitation';
/**
* The ending statuses of an organization invitation per RFC 0003. It means that the invitation
* status cannot be changed anymore.

View file

@ -157,6 +157,12 @@ export const mockEmailConnectorConfig = {
subject: 'Logto Test Template',
content: 'This is for testing purposes only. Your passcode is {{code}}.',
},
{
usageType: 'OrganizationInvitation',
type: 'text/plain',
subject: 'Logto Organization Invitation Template',
content: 'This is for organization invitation purposes only. Your link is {{link}}.',
},
],
};

View file

@ -13,7 +13,7 @@ export type PostOrganizationInvitationData = {
organizationId: string;
expiresAt: number;
organizationRoleIds?: string[];
emailPayload?: SendMessagePayload | false;
messagePayload?: SendMessagePayload | false;
};
export class OrganizationInvitationApi extends ApiFactory<

View file

@ -1,7 +1,7 @@
import fs from 'node:fs/promises';
import { createServer, type RequestListener } from 'node:http';
import { mockConnectorFilePaths } from '@logto/connector-kit';
import { mockConnectorFilePaths, type SendMessagePayload } from '@logto/connector-kit';
import { RequestError } from 'got';
import { createUser } from '#src/api/index.js';
@ -28,6 +28,7 @@ type ConnectorMessageRecord = {
address?: string;
code: string;
type: string;
payload: SendMessagePayload;
};
/**

View file

@ -1,8 +1,11 @@
import assert from 'node:assert';
import { ConnectorType } from '@logto/connector-kit';
import { generateStandardId } from '@logto/shared';
import { HTTPError } from 'got';
import { clearConnectorsByTypes, setEmailConnector } from '#src/helpers/connector.js';
import { readConnectorMessage } from '#src/helpers/index.js';
import { OrganizationApiTest, OrganizationInvitationApiTest } from '#src/helpers/organization.js';
const randomId = () => generateStandardId(4);
@ -36,6 +39,43 @@ describe('organization invitation creation', () => {
});
});
it('should be able to create an invitation with sending email', async () => {
await setEmailConnector();
const organization = await organizationApi.create({ name: 'test' });
await invitationApi.create({
organizationId: organization.id,
invitee: `${randomId()}@example.com`,
expiresAt: Date.now() + 1_000_000,
messagePayload: {
link: 'https://example.com',
},
});
expect(await readConnectorMessage('Email')).toMatchObject({
type: 'OrganizationInvitation',
payload: {
link: 'https://example.com',
},
});
});
it('should throw error if email connector is not set', async () => {
await clearConnectorsByTypes([ConnectorType.Email]);
const organization = await organizationApi.create({ name: 'test' });
const error = await invitationApi
.create({
organizationId: organization.id,
invitee: `${randomId()}@example.com`,
expiresAt: Date.now() + 1_000_000,
messagePayload: {
link: 'https://example.com',
},
})
.catch((error: unknown) => error);
expectErrorResponse(error, 501, 'connector.not_found');
});
it('should not be able to create invitations with the same email', async () => {
const organization = await organizationApi.create({ name: 'test' });
const email = `${randomId()}@example.com`;