Commit 043e23b5 authored by MrMan's avatar MrMan

Add more API tests, fix access bug

parent 18b5aa2e
......@@ -81,7 +81,7 @@ export default {
*/
detectAuthErrorResponse(resp) {
// Attempt to catch failures that need reauthentication
if (resp.status == 401 && AUTHENTICATION_RETRIES < 5) {
if (_.get(resp, "status") == 401 && AUTHENTICATION_RETRIES < 5) {
console.log("[TINDER API] Detected 401 response, possibly bad API token, reauthenticating...");
AUTHENTICATION_RETRIES++;
Actions.authenticateWithTinder();
......
/**
* Modify an async function to disable before/after it runs
*
* @param {function} done - The function that is called when done
* @param {function} fn - The steps to be performed, this function will be passed a modified done()
* @returns {function} fn - The steps to be performed, this function will be passed a modified done()
*/
export function TestWithDisabledConsoleLogging(fn) {
return (done) => {
// Backup and disable console.log
let OLD_CONSOLE_LOG = console.log;
console.log = undefined;
// Augment done function
let newDone = () => {
console.log = OLD_CONSOLE_LOG;
done();
};
// Run the actual function with the modified done callback
fn(newDone);
};
}
import System from "systemjs";
import "../config";
import "should";
import should from "should";
import _ from "lodash";
import { TestWithDisabledConsoleLogging } from "./test-util";
const POST_TEST_URL = "http://nowhere.com/api";
const TEST_TINDER_TOKEN = "1234abcdefghij";
const TEST_URL = "http://nowhere.com/api";
const POST_TEST_PARAMS = {properties: 'present'};
const AUTH_ERROR_RESPONSE = {
status: 401
};
// Mock SystemJS fetch import (mocked fetch will simply return the URL and the options)
const MOCK_FETCH_MODULE = {
......@@ -20,10 +25,11 @@ describe("Tinder API", () => {
let tinderAPI = api.default;
// Perform POST with API, manually verify url and opts that are returned
tinderAPI.POST(POST_TEST_URL, POST_TEST_PARAMS)
tinderAPI.POST(TEST_URL, POST_TEST_PARAMS)
.then((res) => {
res.url.should.be.exactly(POST_TEST_URL);
res.url.should.be.exactly(TEST_URL);
res.opts.should.have.keys("body", "headers", "method");
res.opts.method.should.be.exactly("post");
res.opts.body.should.be.exactly(JSON.stringify(POST_TEST_PARAMS));
done();
})
......@@ -32,4 +38,66 @@ describe("Tinder API", () => {
.catch(done);
});
it("Should include a tinder token when a POST with token is made", (done) => {
System.import("./js/tinder-api")
.then(api => {
let tinderAPI = api.default;
// Perform POST with API, manually verify url and opts that are returned
tinderAPI.POSTWithToken(TEST_URL, POST_TEST_PARAMS, TEST_TINDER_TOKEN)
.then((res) => {
res.url.should.be.exactly(TEST_URL);
res.opts.should.have.keys("body", "headers", "method");
res.opts.body.should.be.exactly(JSON.stringify(POST_TEST_PARAMS));
res.opts.method.should.be.exactly("post");
// Expect token to be present and equal to mock value
should.exist(res.opts.headers['X-Auth-Token']);
res.opts.headers['X-Auth-Token'].should.be.exactly(TEST_TINDER_TOKEN);
done();
})
.catch(done);
})
.catch(done);
});
it("Should include a tinder token when a GET with token is made", (done) => {
System.import("./js/tinder-api")
.then(api => {
let tinderAPI = api.default;
// Perform GET with API, manually verify url and opts that are returned
tinderAPI.GETWithToken(TEST_TINDER_TOKEN, TEST_URL)
.then((res) => {
res.url.should.be.exactly(TEST_URL);
res.opts.should.have.keys("headers");
res.opts.should.not.have.keys("method"); // since GET is default method
// Expect token to be present and equal to mock value
should.exist(res.opts.headers['X-Auth-Token']);
res.opts.headers['X-Auth-Token'].should.be.exactly(TEST_TINDER_TOKEN);
done();
})
.catch(done);
})
.catch(done);
});
it("Should detect authentication error responses properly", TestWithDisabledConsoleLogging((done) => {
System.import("./js/tinder-api")
.then(api => {
let tinderAPI = api.default;
// Authentication retries should increase
let resp = {data: "Working response"};
let noErrorResponse = tinderAPI.detectAuthErrorResponse(resp);
let errorResponse = tinderAPI.detectAuthErrorResponse(AUTH_ERROR_RESPONSE);
noErrorResponse.should.eql(resp);
should.not.exist(errorResponse);
done();
})
.catch(done);
}));
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment