Compare commits

...

14 Commits

Author SHA1 Message Date
Rıdvan Akca
1893d9ea56 docs(hubspot): update hubspot connection 2024-02-27 13:05:42 +03:00
Ömer Faruk Aydın
636870a075 Merge pull request #1661 from automatisch/get-app
feat: Introduce app serializer
2024-02-26 22:32:06 +01:00
Faruk AYDIN
8981174302 feat: Introduce app serializer 2024-02-26 22:25:03 +01:00
Faruk AYDIN
dd5f05334b feat: Allow renderer to use explicitly defined serializers 2024-02-26 22:17:21 +01:00
Ömer Faruk Aydın
929b626b51 Merge pull request #1660 from automatisch/rest-get-app
feat: Implement get app API endpoint
2024-02-26 21:44:23 +01:00
Faruk AYDIN
7d5b2ec81e feat: Implement get app API endpoint 2024-02-26 17:59:48 +01:00
Ömer Faruk Aydın
f0e2d36c34 Merge pull request #1657 from automatisch/timestamp-serializer
feat: Use timestamp for serializer date objects
2024-02-26 14:36:34 +01:00
Faruk AYDIN
94f171d757 feat: Use timestamp for serializer date objects 2024-02-26 14:11:56 +01:00
Ömer Faruk Aydın
04e06db430 Merge pull request #1656 from automatisch/api-controller-tests
test: Cover not found responses for API endpoint tests
2024-02-26 13:36:48 +01:00
Faruk AYDIN
d74b215169 test: Cover bad request responses for API endpoint tests 2024-02-26 13:30:30 +01:00
Faruk AYDIN
404ea94dd2 test: Cover not found responses for API endpoint tests 2024-02-26 01:40:20 +01:00
Faruk AYDIN
4afe7c6b46 feat: Handle bad request for invalid UUID 2024-02-26 01:26:04 +01:00
Faruk AYDIN
58658c6b1a feat: Do not expose unknown error message to client 2024-02-26 01:07:31 +01:00
Faruk AYDIN
ec444317b3 feat: Catch not found error message for objection 2024-02-26 01:06:54 +01:00
28 changed files with 317 additions and 55 deletions

View File

@@ -1,5 +1,6 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../../app.js';
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
import { createUser } from '../../../../../../test/factories/user.js';
@@ -31,5 +32,21 @@ describe('GET /api/v1/admin/app-auth-clients/:appAuthClientId', () => {
const expectedPayload = getAdminAppAuthClientMock(currentAppAuthClient);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing app auth client UUID', async () => {
const notExistingAppAuthClientUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/admin/app-auth-clients/${notExistingAppAuthClientUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.get('/api/v1/admin/app-auth-clients/invalidAppAuthClientUUID')
.set('Authorization', token)
.expect(400);
});
});
});

View File

@@ -1,5 +1,6 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../../app.js';
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
import { createRole } from '../../../../../../test/factories/role.js';
@@ -20,7 +21,7 @@ describe('GET /api/v1/admin/roles/:roleId', () => {
token = createAuthTokenByUserId(currentUser.id);
});
it('should return roles', async () => {
it('should return role', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
const response = await request(app)
@@ -35,4 +36,24 @@ describe('GET /api/v1/admin/roles/:roleId', () => {
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing role UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
const notExistingRoleUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/admin/roles/${notExistingRoleUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
await request(app)
.get('/api/v1/admin/roles/invalidRoleUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -1,5 +1,6 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../../app.js';
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
import { createRole } from '../../../../../../test/factories/role.js';
@@ -31,4 +32,26 @@ describe('GET /api/v1/admin/saml-auth-provider/:samlAuthProviderId', () => {
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing saml auth provider UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
const notExistingSamlAuthProviderUUID = Crypto.randomUUID();
await request(app)
.get(
`/api/v1/admin/saml-auth-providers/${notExistingSamlAuthProviderUUID}`
)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
await request(app)
.get('/api/v1/admin/saml-auth-providers/invalidSamlAuthProviderUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -1,5 +1,6 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../../app.js';
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id';
import { createUser } from '../../../../../../test/factories/user';
@@ -31,4 +32,24 @@ describe('GET /api/v1/admin/users/:userId', () => {
const expectedPayload = getUserMock(anotherUser, anotherUserRole);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing user UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
const notExistingUserUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/admin/users/${notExistingUserUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
await request(app)
.get('/api/v1/admin/users/invalidUserUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -1,5 +1,6 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js';
import { createUser } from '../../../../../test/factories/user.js';
@@ -28,4 +29,20 @@ describe('GET /api/v1/app-auth-clients/:id', () => {
const expectedPayload = getAppAuthClientMock(currentAppAuthClient);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing app auth client ID', async () => {
const notExistingAppAuthClientUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/app-auth-clients/${notExistingAppAuthClientUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.get('/api/v1/app-auth-clients/invalidAppAuthClientUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -0,0 +1,8 @@
import App from '../../../../models/app.js';
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
const app = await App.findOneByKey(request.params.appKey);
renderObject(response, app, { serializer: 'App' });
};

View File

@@ -0,0 +1,35 @@
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import App from '../../../../models/app';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
import { createUser } from '../../../../../test/factories/user';
import getAppMock from '../../../../../test/mocks/rest/api/v1/apps/get-app.js';
describe('GET /api/v1/apps/:appKey', () => {
let currentUser, token;
beforeEach(async () => {
currentUser = await createUser();
token = createAuthTokenByUserId(currentUser.id);
});
it('should return the app info', async () => {
const exampleApp = await App.findOneByKey('github');
const response = await request(app)
.get(`/api/v1/apps/${exampleApp.key}`)
.set('Authorization', token)
.expect(200);
const expectedPayload = getAppMock(exampleApp);
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for invalid app key', async () => {
await request(app)
.get('/api/v1/apps/invalid-app-key')
.set('Authorization', token)
.expect(404);
});
});

View File

@@ -1,5 +1,6 @@
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import Crypto from 'crypto';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
import { createUser } from '../../../../../test/factories/user';
@@ -68,4 +69,34 @@ describe('GET /api/v1/flows/:flowId', () => {
expect(response.body).toEqual(expectedPayload);
});
it('should return not found response for not existing flow UUID', async () => {
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
const notExistingFlowUUID = Crypto.randomUUID();
await request(app)
.get(`/api/v1/flows/${notExistingFlowUUID}`)
.set('Authorization', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: [],
});
await request(app)
.get('/api/v1/flows/invalidFlowUUID')
.set('Authorization', token)
.expect(400);
});
});

View File

@@ -1,14 +1,31 @@
import logger from './logger.js';
import objection from 'objection';
const { NotFoundError, DataError } = objection;
// Do not remove `next` argument as the function signature will not fit for an error handler middleware
// eslint-disable-next-line no-unused-vars
const errorHandler = (err, req, res, next) => {
if (err.message === 'Not Found') {
res.status(404).end();
} else {
logger.error(err.message + '\n' + err.stack);
res.status(err.statusCode || 500).send(err.message);
const errorHandler = (error, request, response, next) => {
if (error.message === 'Not Found' || error instanceof NotFoundError) {
response.status(404).end();
}
if (notFoundAppError(error)) {
response.status(404).end();
}
if (error instanceof DataError) {
response.status(400).end();
}
logger.error(error.message + '\n' + error.stack);
response.status(error.statusCode || 500).end();
};
const notFoundAppError = (error) => {
return (
error.message.includes('An application with the') ||
error.message.includes("key couldn't be found.")
);
};
export default errorHandler;

View File

@@ -11,7 +11,7 @@ const isArray = (object) =>
const totalCount = (object) =>
isPaginated(object) ? object.totalCount : isArray(object) ? object.length : 1;
const renderObject = (response, object) => {
const renderObject = (response, object, options) => {
let data = isPaginated(object) ? object.records : object;
const type = isPaginated(object)
@@ -20,7 +20,9 @@ const renderObject = (response, object) => {
? object?.[0]?.constructor?.name || 'Object'
: object.constructor.name;
const serializer = serializers[type];
const serializer = options?.serializer
? serializers[options.serializer]
: serializers[type];
if (serializer) {
data = Array.isArray(data)

View File

@@ -0,0 +1,10 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import getAppAction from '../../../controllers/api/v1/apps/get-app.js';
const router = Router();
router.get('/:appKey', authenticateUser, asyncHandler(getAppAction));
export default router;

View File

@@ -8,6 +8,7 @@ import usersRouter from './api/v1/users.js';
import paymentRouter from './api/v1/payment.ee.js';
import appAuthClientsRouter from './api/v1/app-auth-clients.js';
import flowsRouter from './api/v1/flows.js';
import appsRouter from './api/v1/apps.js';
import samlAuthProvidersRouter from './api/v1/admin/saml-auth-providers.ee.js';
import rolesRouter from './api/v1/admin/roles.ee.js';
import permissionsRouter from './api/v1/admin/permissions.ee.js';
@@ -25,6 +26,7 @@ router.use('/api/v1/users', usersRouter);
router.use('/api/v1/payment', paymentRouter);
router.use('/api/v1/app-auth-clients', appAuthClientsRouter);
router.use('/api/v1/flows', flowsRouter);
router.use('/api/v1/apps', appsRouter);
router.use('/api/v1/admin/saml-auth-providers', samlAuthProvidersRouter);
router.use('/api/v1/admin/roles', rolesRouter);
router.use('/api/v1/admin/permissions', permissionsRouter);

View File

@@ -0,0 +1,12 @@
const appSerializer = (app) => {
return {
name: app.name,
key: app.key,
iconUrl: app.iconUrl,
authDocUrl: app.authDocUrl,
supportsConnections: app.supportsConnections,
primaryColor: app.primaryColor,
};
};
export default appSerializer;

View File

@@ -0,0 +1,20 @@
import { describe, it, expect } from 'vitest';
import App from '../models/app';
import appSerializer from './app';
describe('appSerializer', () => {
it('should return permission data', async () => {
const app = await App.findOneByKey('deepl');
const expectedPayload = {
name: app.name,
key: app.key,
iconUrl: app.iconUrl,
authDocUrl: app.authDocUrl,
supportsConnections: app.supportsConnections,
primaryColor: app.primaryColor,
};
expect(appSerializer(app)).toEqual(expectedPayload);
});
});

View File

@@ -5,6 +5,7 @@ import samlAuthProviderSerializer from './saml-auth-provider.ee.js';
import appAuthClientSerializer from './app-auth-client.js';
import flowSerializer from './flow.js';
import stepSerializer from './step.js';
import appSerializer from './app.js';
const serializers = {
User: userSerializer,
@@ -14,6 +15,7 @@ const serializers = {
AppAuthClient: appAuthClientSerializer,
Flow: flowSerializer,
Step: stepSerializer,
App: appSerializer,
};
export default serializers;

View File

@@ -5,8 +5,8 @@ const permissionSerializer = (permission) => {
action: permission.action,
subject: permission.subject,
conditions: permission.conditions,
createdAt: permission.createdAt,
updatedAt: permission.updatedAt,
createdAt: permission.createdAt.getTime(),
updatedAt: permission.updatedAt.getTime(),
};
};

View File

@@ -16,8 +16,8 @@ describe('permissionSerializer', () => {
action: permission.action,
subject: permission.subject,
conditions: permission.conditions,
createdAt: permission.createdAt,
updatedAt: permission.updatedAt,
createdAt: permission.createdAt.getTime(),
updatedAt: permission.updatedAt.getTime(),
};
expect(permissionSerializer(permission)).toEqual(expectedPayload);

View File

@@ -6,8 +6,8 @@ const roleSerializer = (role) => {
name: role.name,
key: role.key,
description: role.description,
createdAt: role.createdAt,
updatedAt: role.updatedAt,
createdAt: role.createdAt.getTime(),
updatedAt: role.updatedAt.getTime(),
isAdmin: role.isAdmin,
};

View File

@@ -29,8 +29,8 @@ describe('roleSerializer', () => {
name: role.name,
key: role.key,
description: role.description,
createdAt: role.createdAt,
updatedAt: role.updatedAt,
createdAt: role.createdAt.getTime(),
updatedAt: role.updatedAt.getTime(),
isAdmin: role.isAdmin,
};

View File

@@ -6,8 +6,8 @@ const userSerializer = (user) => {
let userData = {
id: user.id,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
createdAt: user.createdAt.getTime(),
updatedAt: user.updatedAt.getTime(),
fullName: user.fullName,
};

View File

@@ -31,11 +31,11 @@ describe('userSerializer', () => {
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
const expectedPayload = {
createdAt: user.createdAt,
createdAt: user.createdAt.getTime(),
email: user.email,
fullName: user.fullName,
id: user.id,
updatedAt: user.updatedAt,
updatedAt: user.updatedAt.getTime(),
};
expect(userSerializer(user)).toEqual(expectedPayload);
@@ -67,7 +67,7 @@ describe('userSerializer', () => {
it('should return user data with trial expiry date', async () => {
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
await user.$query().patch({
await user.$query().patchAndFetch({
trialExpiryDate: DateTime.now().plus({ days: 30 }).toISODate(),
});

View File

@@ -5,16 +5,16 @@ const getRoleMock = async (role, permissions) => {
name: role.name,
isAdmin: role.isAdmin,
description: role.description,
createdAt: role.createdAt.toISOString(),
updatedAt: role.updatedAt.toISOString(),
createdAt: role.createdAt.getTime(),
updatedAt: role.updatedAt.getTime(),
permissions: permissions.map((permission) => ({
id: permission.id,
action: permission.action,
conditions: permission.conditions,
roleId: permission.roleId,
subject: permission.subject,
createdAt: permission.createdAt.toISOString(),
updatedAt: permission.updatedAt.toISOString(),
createdAt: permission.createdAt.getTime(),
updatedAt: permission.updatedAt.getTime(),
})),
};

View File

@@ -6,8 +6,8 @@ const getRolesMock = async (roles) => {
name: role.name,
isAdmin: role.isAdmin,
description: role.description,
createdAt: role.createdAt.toISOString(),
updatedAt: role.updatedAt.toISOString(),
createdAt: role.createdAt.getTime(),
updatedAt: role.updatedAt.getTime(),
};
});

View File

@@ -1,21 +1,21 @@
const getUserMock = (currentUser, role) => {
return {
data: {
createdAt: currentUser.createdAt.toISOString(),
createdAt: currentUser.createdAt.getTime(),
email: currentUser.email,
fullName: currentUser.fullName,
id: currentUser.id,
role: {
createdAt: role.createdAt.toISOString(),
createdAt: role.createdAt.getTime(),
description: null,
id: role.id,
isAdmin: role.isAdmin,
key: role.key,
name: role.name,
updatedAt: role.updatedAt.toISOString(),
updatedAt: role.updatedAt.getTime(),
},
trialExpiryDate: currentUser.trialExpiryDate.toISOString(),
updatedAt: currentUser.updatedAt.toISOString(),
updatedAt: currentUser.updatedAt.getTime(),
},
meta: {
count: 1,

View File

@@ -3,23 +3,23 @@ const getUsersMock = async (users, roles) => {
const role = roles.find((r) => r.id === user.roleId);
return {
createdAt: user.createdAt.toISOString(),
createdAt: user.createdAt.getTime(),
email: user.email,
fullName: user.fullName,
id: user.id,
role: role
? {
createdAt: role.createdAt.toISOString(),
createdAt: role.createdAt.getTime(),
description: role.description,
id: role.id,
isAdmin: role.isAdmin,
key: role.key,
name: role.name,
updatedAt: role.updatedAt.toISOString(),
updatedAt: role.updatedAt.getTime(),
}
: null,
trialExpiryDate: user.trialExpiryDate.toISOString(),
updatedAt: user.updatedAt.toISOString(),
updatedAt: user.updatedAt.getTime(),
};
});

View File

@@ -0,0 +1,21 @@
const getAppMock = (app) => {
return {
data: {
authDocUrl: app.authDocUrl,
iconUrl: app.iconUrl,
key: app.key,
name: app.name,
primaryColor: app.primaryColor,
supportsConnections: app.supportsConnections,
},
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'Object',
},
};
};
export default getAppMock;

View File

@@ -1,22 +1,22 @@
const getCurrentUserMock = (currentUser, role) => {
return {
data: {
createdAt: currentUser.createdAt.toISOString(),
createdAt: currentUser.createdAt.getTime(),
email: currentUser.email,
fullName: currentUser.fullName,
id: currentUser.id,
permissions: [],
role: {
createdAt: role.createdAt.toISOString(),
createdAt: role.createdAt.getTime(),
description: null,
id: role.id,
isAdmin: role.isAdmin,
key: role.key,
name: role.name,
updatedAt: role.updatedAt.toISOString(),
updatedAt: role.updatedAt.getTime(),
},
trialExpiryDate: currentUser.trialExpiryDate.toISOString(),
updatedAt: currentUser.updatedAt.toISOString(),
updatedAt: currentUser.updatedAt.getTime(),
},
meta: {
count: 1,

View File

@@ -5,18 +5,21 @@ This page explains the steps you need to follow to set up the Hubspot connection
:::
1. Go to the [HubSpot Developer page](https://developers.hubspot.com/).
2. Login into your developer account.
3. Click on the **Manage apps** button.
4. Click on the **Create app** button.
5. Fill the **Public app name** field with the name of your API app.
6. Go to the **Auth** tab.
7. Fill the **Redirect URL(s)** field with the OAuth Redirect URL from the Automatisch connection creation page.
8. Go to the **Scopes** tab.
9. Select the scopes you want to use with Automatisch.
10. Click on the **Create App** button.
11. Go back to the **Auth** tab.
12. Copy the **Client ID** and **Client Secret** values.
13. Paste the **Client ID** value into Automatisch as **Client ID**, respectively.
14. Paste the **Client Secret** value into Automatisch as **Client Secret**, respectively.
15. Click the **Submit** button on Automatisch.
16. Now, you can start using the HubSpot connection with Automatisch.
2. Click on the **Create a developer account** button.
3. Click on the **Create App Developer account** button.
4. Fill the form.
5. Login into your developer account.
6. Click on the **Manage apps** button.
7. Click on the **Create app** button.
8. Fill the **Public app name** field with the name of your API app.
9. Go to the **Auth** tab.
10. Fill the **Redirect URL(s)** field with the OAuth Redirect URL from the Automatisch connection creation page.
11. Go to the **Scopes** tab.
12. Select the scopes you want to use with Automatisch.
13. Click on the **Create App** button.
14. Go back to the **Auth** tab.
15. Copy the **Client ID** and **Client Secret** values.
16. Paste the **Client ID** value into Automatisch as **Client ID**, respectively.
17. Paste the **Client Secret** value into Automatisch as **Client Secret**, respectively.
18. Click the **Submit** button on Automatisch.
19. Now, you can start using the HubSpot connection with Automatisch.