Skip to content

Commit

Permalink
checking slack token in index handler, extracting env vars and slack …
Browse files Browse the repository at this point in the history
…body properties
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Aug 12, 2017
1 parent a3c5720 commit da743bd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 13 deletions.
45 changes: 38 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
const { response } = require('./util/lambda')
const { parseFormString } = require('./util/parse');
const {
INVALID_TOKEN,
TYPE_PRIVATE,
TYPE_PUBLIC,
slackResp
} = require('./slack');


function handler(event, context, callback) {

const { body } = event;
const { SPOTIFY_CLIENT_ID, SPOTIFY_SECRET } = process.env;
const { body = '' } = event;
const {
SPOTIFY_USER_ACCESS_TOKEN,
SPOTIFY_LOCAL_URL,
SLACK_TOKEN,
SPOTIFY_RADIO_PLAYLIST
} = process.env;

const {
text,
token,
user_name
} = parseFormString(event.body);

if (token !== SLACK_TOKEN) {
callback(null,
slackResp(
INVALID_TOKEN,
401,
TYPE_PRIVATE
)
);
} else {

callback(null, response({
message: 'It works!',
body: body
}));
// -- do stuff
callback(null,
slackResp(
'It works.'
)
);
}
}

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions src/slack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { response } = require('../util/lambda')

// response types
const TYPE_PRIVATE = 'ephemeral';
const TYPE_PUBLIC = 'in_channel';

// error messages
const INVALID_TOKEN = 'Slack token is invalid.';

function slackResp(text, code = 200, type = TYPE_PUBLIC) {
return response({
// eslint-disable-next-line camelcase
response_type: type,
text
}, code);
}

module.exports = {
TYPE_PUBLIC,
TYPE_PRIVATE,
INVALID_TOKEN,
slackResp
};
47 changes: 41 additions & 6 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { beforeEach, afterEach, describe, it } = require('mocha');
const { expect, config } = require('chai');
const request = require('request-promise-native');
const sinon = require('sinon');

const dotenv = require('dotenv');
const { INVALID_TOKEN } = require('../src/slack');
const { handler } = require('../src');

const context = describe;
Expand All @@ -12,11 +13,45 @@ const context = describe;
config.includeStack = true;
// uncomment to test with credentials from .env
// dotenv.config();
dotenv.config({
path: PATH.resolve(__dirname, '../', 'test/.test-env')
});

describe('The Index Lambda Handler', () => {
context('with an request event', () => {

const event = { body: {} };
context('with a request event without a slack token', () => {
const event = { };

it('sends a response body that can be parsed as JSON ', (done) => {
handler(event, {}, (err, resp) => {
try {
const { text } = JSON.parse(resp.body);
expect(text)
.to.eq(INVALID_TOKEN);
done()
} catch (error) {
done(error);
}
});
});

it('sends a responseCode 401', (done) => {
handler(event, {}, (err, resp) => {
try {
expect(resp.statusCode)
.to.eq(401);
done()
} catch (error) {
done(error);
}
});
});
});

context('with an request event with a valid token', () => {

const slackBody = `text=foo&token=${process.env.SLACK_TOKEN}`;
const event = { body: slackBody };

it('sends a response body', (done) => {
handler(event, {}, (err, resp) => {
Expand All @@ -33,9 +68,9 @@ describe('The Index Lambda Handler', () => {
it('sends a response body that can be parsed as JSON ', (done) => {
handler(event, {}, (err, resp) => {
try {
const { message } = JSON.parse(resp.body);
expect(message)
.to.eq('It works!');
const { text } = JSON.parse(resp.body);
expect(text)
.to.eq('It works.');
done()
} catch (error) {
done(error);
Expand Down

0 comments on commit da743bd

Please sign in to comment.