2023-09-04 06:00:25 -07:00
|
|
|
import type { Credentials } from '@/requests';
|
|
|
|
import type { User } from '@db/entities/User';
|
2023-11-08 07:29:39 -08:00
|
|
|
import * as testDb from './shared/testDb';
|
|
|
|
import { setupTestServer } from './shared/utils/';
|
|
|
|
import { randomCredentialPayload as payload } from './shared/random';
|
|
|
|
import { saveCredential } from './shared/db/credentials';
|
|
|
|
import { createMember, createOwner } from './shared/db/users';
|
|
|
|
import { getCredentialOwnerRole } from './shared/db/roles';
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const { any } = expect;
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const testServer = setupTestServer({ endpointGroups: ['credentials'] });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
let owner: User;
|
|
|
|
let member: User;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await testDb.truncate(['SharedCredentials', 'Credentials']);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
owner = await createOwner();
|
|
|
|
member = await createMember();
|
2023-09-04 06:00:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
type GetAllResponse = { body: { data: Credentials.WithOwnedByAndSharedWith[] } };
|
|
|
|
|
|
|
|
describe('GET /credentials', () => {
|
|
|
|
describe('should return', () => {
|
|
|
|
test('all credentials for owner', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const { id: id1 } = await saveCredential(payload(), { user: owner, role });
|
|
|
|
const { id: id2 } = await saveCredential(payload(), { user: member, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(2);
|
|
|
|
|
|
|
|
response.body.data.forEach(validateCredential);
|
|
|
|
|
2023-09-19 06:42:39 -07:00
|
|
|
const savedIds = [id1, id2].sort();
|
|
|
|
const returnedIds = response.body.data.map((c) => c.id).sort();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
expect(savedIds).toEqual(returnedIds);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('only own credentials for member', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const firstMember = member;
|
2023-11-08 07:29:39 -08:00
|
|
|
const secondMember = await createMember();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const c1 = await saveCredential(payload(), { user: firstMember, role });
|
|
|
|
const c2 = await saveCredential(payload(), { user: secondMember, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(firstMember)
|
|
|
|
.get('/credentials')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
const [firstMemberCred] = response.body.data;
|
|
|
|
|
|
|
|
validateCredential(firstMemberCred);
|
|
|
|
expect(firstMemberCred.id).toBe(c1.id);
|
|
|
|
expect(firstMemberCred.id).not.toBe(c2.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('filter', () => {
|
|
|
|
test('should filter credentials by field: name - full match', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
|
|
|
const savedCred = await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query(`filter={ "name": "${savedCred.name}" }`)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
const [returnedCred] = response.body.data;
|
|
|
|
|
|
|
|
expect(returnedCred.name).toBe(savedCred.name);
|
|
|
|
|
|
|
|
const _response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('filter={ "name": "Non-Existing Credential" }')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(_response.body.data).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter credentials by field: name - partial match', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
|
|
|
const savedCred = await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const partialName = savedCred.name.slice(3);
|
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query(`filter={ "name": "${partialName}" }`)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
const [returnedCred] = response.body.data;
|
|
|
|
|
|
|
|
expect(returnedCred.name).toBe(savedCred.name);
|
|
|
|
|
|
|
|
const _response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('filter={ "name": "Non-Existing Credential" }')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(_response.body.data).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter credentials by field: type - full match', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const savedCred = await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query(`filter={ "type": "${savedCred.type}" }`)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
const [returnedCred] = response.body.data;
|
|
|
|
|
|
|
|
expect(returnedCred.type).toBe(savedCred.type);
|
|
|
|
|
|
|
|
const _response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('filter={ "type": "Non-Existing Credential" }')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(_response.body.data).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should filter credentials by field: type - partial match', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const savedCred = await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const partialType = savedCred.type.slice(3);
|
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query(`filter={ "type": "${partialType}" }`)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
const [returnedCred] = response.body.data;
|
|
|
|
|
|
|
|
expect(returnedCred.type).toBe(savedCred.type);
|
|
|
|
|
|
|
|
const _response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('filter={ "type": "Non-Existing Credential" }')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(_response.body.data).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('select', () => {
|
|
|
|
test('should select credential field: id', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await saveCredential(payload(), { user: owner, role });
|
|
|
|
await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('select=["id"]')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
data: [{ id: any(String) }, { id: any(String) }],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should select credential field: name', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await saveCredential(payload(), { user: owner, role });
|
|
|
|
await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('select=["name"]')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
data: [{ name: any(String) }, { name: any(String) }],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should select credential field: type', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await saveCredential(payload(), { user: owner, role });
|
|
|
|
await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response: GetAllResponse = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('select=["type"]')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
data: [{ type: any(String) }, { type: any(String) }],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('take', () => {
|
|
|
|
test('should return n credentials or less, without skip', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await saveCredential(payload(), { user: owner, role });
|
|
|
|
await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('take=2')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(2);
|
|
|
|
|
|
|
|
response.body.data.forEach(validateCredential);
|
|
|
|
|
|
|
|
const _response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('take=1')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(_response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
_response.body.data.forEach(validateCredential);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return n credentials or less, with skip', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const role = await getCredentialOwnerRole();
|
2023-09-04 06:00:25 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await saveCredential(payload(), { user: owner, role });
|
|
|
|
await saveCredential(payload(), { user: owner, role });
|
2023-09-04 06:00:25 -07:00
|
|
|
|
|
|
|
const response = await testServer
|
|
|
|
.authAgentFor(owner)
|
|
|
|
.get('/credentials')
|
|
|
|
.query('take=1&skip=1')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
expect(response.body.data).toHaveLength(1);
|
|
|
|
|
|
|
|
response.body.data.forEach(validateCredential);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function validateCredential(credential: Credentials.WithOwnedByAndSharedWith) {
|
|
|
|
const { name, type, nodesAccess, sharedWith, ownedBy } = credential;
|
|
|
|
|
|
|
|
expect(typeof name).toBe('string');
|
|
|
|
expect(typeof type).toBe('string');
|
|
|
|
expect(typeof nodesAccess[0].nodeType).toBe('string');
|
|
|
|
expect('data' in credential).toBe(false);
|
|
|
|
|
|
|
|
if (sharedWith) expect(Array.isArray(sharedWith)).toBe(true);
|
|
|
|
|
|
|
|
if (ownedBy) {
|
|
|
|
const { id, email, firstName, lastName } = ownedBy;
|
|
|
|
|
|
|
|
expect(typeof id).toBe('string');
|
|
|
|
expect(typeof email).toBe('string');
|
|
|
|
expect(typeof firstName).toBe('string');
|
|
|
|
expect(typeof lastName).toBe('string');
|
|
|
|
}
|
|
|
|
}
|