Skip to content

Commit

Permalink
Merge pull request #153 from biomage-org/remove-sns-from-post-registr…
Browse files Browse the repository at this point in the history
…ation

Remove SNS from post-registration
  • Loading branch information
aerlaut authored Jun 9, 2023
2 parents 0b5ef5b + ca6c405 commit 5daa0fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 49 deletions.
22 changes: 4 additions & 18 deletions src/api.v2/helpers/access/postRegistrationHandler.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
const parseSNSMessage = require('../../../utils/parseSNSMessage');
const getLogger = require('../../../utils/getLogger');
const { OK } = require('../../../utils/responses');

const UserAccess = require('../../model/UserAccess');
const snsTopics = require('../../../config/snsTopics');

const logger = getLogger('[PostRegistrationHandler] - ');

const postRegistrationHandler = async (req) => {
let data;
let messageType;

try {
const { parsedMessage, msg } = await parseSNSMessage(req, snsTopics.POST_REGISTRATION);
data = parsedMessage;
messageType = msg.Type;
} catch (e) {
logger.error('Parsing initial SNS message failed:', e);
return;
}

// If this is a subscription confirmation, we can just return.
if (messageType !== 'Notification') return;

const { userEmail, userId } = data;
const { userEmail, userId } = req.body;

new UserAccess().registerNewUserAccess(userEmail, userId);

logger.log(`Post registration handled for user ${userId}`);

return OK();
};

module.exports = postRegistrationHandler;
36 changes: 5 additions & 31 deletions tests/api.v2/helpers/access/postRegistrationHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
const UserAccess = require('../../../../src/api.v2/model/UserAccess');

const postRegistrationHandler = require('../../../../src/api.v2/helpers/access/postRegistrationHandler');
const parseSNSMessage = require('../../../../src/utils/parseSNSMessage');
const { OK } = require('../../../../src/utils/responses');

jest.mock('../../../../src/utils/parseSNSMessage');
jest.mock('../../../../src/api.v2/model/UserAccess');

const mockUserAccess = {
Expand All @@ -14,8 +13,6 @@ const mockUserAccess = {

UserAccess.mockReturnValue(mockUserAccess);

const experimentId = 'experimentId';

describe('postRegistrationHandler', () => {
beforeEach(async () => {
jest.clearAllMocks();
Expand All @@ -25,41 +22,18 @@ describe('postRegistrationHandler', () => {
const mockUserEmail = 'mock-user-email';
const mockUserId = 'mock-user-email';

const mockMessage = {
msg: { Type: 'Notification' },
parsedMessage: {
const mockReq = {
body: {
userEmail: mockUserEmail,
userId: mockUserId,
},
};

parseSNSMessage.mockImplementationOnce(() => Promise.resolve(mockMessage));

await postRegistrationHandler();
const res = await postRegistrationHandler(mockReq);

expect(mockUserAccess.registerNewUserAccess).toHaveBeenCalledWith(mockUserEmail, mockUserId);
expect(mockUserAccess.registerNewUserAccess).toHaveBeenCalledTimes(1);
});

it('Does not proceed to registration if SNS message is not notification', async () => {
const mockSubscriptionConfirmation = {
msg: {
type: 'SubscriptionConfirmation',
},
};

parseSNSMessage.mockImplementationOnce(() => Promise.resolve(mockSubscriptionConfirmation));

await postRegistrationHandler(experimentId);

expect(mockUserAccess.registerNewUserAccess).not.toHaveBeenCalled();
});

it('Does not do anything on invalid SNS message', async () => {
parseSNSMessage.mockImplementationOnce(() => Promise.reject(new Error('Invalid SNS message')));

await postRegistrationHandler(experimentId);

expect(mockUserAccess.registerNewUserAccess).not.toHaveBeenCalled();
expect(res).toEqual(OK());
});
});

0 comments on commit 5daa0fd

Please sign in to comment.