Skip to content

Commit

Permalink
wiring up the slack endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Aug 14, 2017
1 parent 56b32f6 commit 3a378e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
28 changes: 20 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const { parseFormString } = require('./util/parse');
const {
INVALID_TOKEN,
TYPE_PRIVATE,
TYPE_PUBLIC,
slackResp
} = require('./slack');
const { convertLinkToUri } = require('./spotify');
const radio = require('./spotify/radio');


function handler(event, context, callback) {
Expand All @@ -20,8 +21,7 @@ function handler(event, context, callback) {
const {
text,
token,
user_name
} = parseFormString(event.body);
} = parseFormString(body);

if (token !== SLACK_TOKEN) {
callback(null,
Expand All @@ -33,12 +33,24 @@ function handler(event, context, callback) {
);
} else {

// -- do stuff
callback(null,
slackResp(
'It works.'
const trackUri = convertLinkToUri(text);
radio
.playBasedOnTrack(
SPOTIFY_RADIO_PLAYLIST,
trackUri,
SPOTIFY_USER_ACCESS_TOKEN,
SPOTIFY_LOCAL_URL
)
);
.then((msg) => {
callback(null,
slackResp(msg)
);
})
.catch((error) => {
callback(null,
slackResp(error.message)
);
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/.test-env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SLACK_TOKEN=foo_bar_baz
EXPIRED_SPOTIFY_ACCESS_TOKEN=BQANyQM3QT080jey_TnbnVSvF1Xr9lKrEc31YkiOlENYwKDuv-3261TtzAWoB0v80IXIPuscAOz7lfTXyfr6WHqCMbAa4DUhRJicDzW1kZ7UV6ofNGWXwoB8raB8ut7q9STN0yIrkvlLvgEAGd6Wsu6Ibfo1olJUDJ0Q2VCAIaNrVtG852AhMp_Wpjzs-K74CYpxO2AtYYNcXo3s0WA7gao84eh1qTnqhprnSbGIjLShqVsmZTo
SPOTIFY_USER_ACCESS_TOKEN=BQDTogSvU_-bQTUV_pzv8LwP7GbMPJmU8MXnrDJWaOp8MXRnoCfslf3gKvlf6CRJhXAfXkvGLFPYUCdZXOFUQolxaYykumLZSPGI1z01AV8c85yMofLZw2-9nj-hHo4y_PORzP_izLnJNxiqgONjSiC5YBaYcfSuv9Fg5S0OFu1pFeOlqWU7M7EVGLMs6SMfH_JMpMhe70OEEwV-7zp9JlkrzTMTXzW69EjLCJ2umsoZvThxUK0
SPOTIFY_LOCAL_URL=http://localhost:5000
SPOTIFY_RADIO_PLAYLIST=spotify:user:awpoops:playlist:5PP1I2m0uxEBb3VKLhI7bP
SPOTIFY_RADIO_PLAYLIST=spotify:user:awpoops:playlist:5PP1I2m0uxEBb3VKLhI7bP
SPOTIFY_USER_ACCESS_TOKEN=BQDeNBJSIWXe3Ezx3IYKf95NJIWeA82cIOFgnDgcc0IwbblLBpi3qEAXz-0R59yAXZ4s4SYz1phS6fWHSWVkDxo08KfSZCUpA-0xEM9FUom7QII2_DL3Ti293Zrhiz1jL4fC-dGLiRxtr7NjImZntCUcss4fBxgrVjuhwCPP4T3AMOljBkE4vY5VNyWq_Pf4aRF1JshyfNg6KLJiL-xClp_AUMglH60IgsjerXWYFq3pt-b8H_U
22 changes: 18 additions & 4 deletions test/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ const sinon = require('sinon');
const dotenv = require('dotenv');
const { INVALID_TOKEN } = require('../src/slack');
const { handler } = require('../src');

const radio = require('../src/spotify/radio');
const context = describe;


config.includeStack = true;
// uncomment to test with credentials from .env
// dotenv.config();
dotenv.config({
path: PATH.resolve(__dirname, '../', 'test/.test-env')
});
Expand Down Expand Up @@ -50,8 +49,22 @@ describe('The Index Lambda Handler', () => {

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

const slackBody = `text=foo&token=${process.env.SLACK_TOKEN}`;
const spotifyTrack = 'spotify:track:2771LMNxwf62FTAdpJMQfM';
const slackBody = `text=${spotifyTrack}&token=foo_bar_baz`;
const event = { body: slackBody };
const respMsg = radio.SLACK_SUCCESS_MESSAGE(
'Radio: based on "Bodak Yellow" by Cardi B'
);

beforeEach(() => {
sinon
.stub(radio, 'playBasedOnTrack')
.resolves(respMsg);
});

afterEach(() => {
radio.playBasedOnTrack.restore();
});

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

0 comments on commit 3a378e3

Please sign in to comment.