Tests pass

pull/498/head
Rick Carlino 2017-10-12 18:36:19 -05:00
parent 18584552f8
commit 562aead395
7 changed files with 78 additions and 49 deletions

View File

@ -12,7 +12,8 @@ module CeleryScriptSettingsBag
move_relative write_pin read_pin send_message
factory_reset execute_script set_user_env wait
add_point install_farmware update_farmware zero
remove_farmware take_photo data_update find_home)
remove_farmware take_photo data_update find_home
install_first_party_farmware)
ALLOWED_PACKAGES = %w(farmbot_os arduino_firmware)
ALLOWED_CHAGES = %w(add remove update)
RESOURCE_NAME = %w(images plants regimens peripherals

View File

@ -1,38 +1,47 @@
jest.mock("axios", () => ({
default: {
get() {
return Promise.reject("NO");
}
interceptors: {
response: { use: jest.fn() },
request: { use: jest.fn() }
},
get() { return Promise.reject("NO"); }
}
}));
import { AuthState } from "../auth/interfaces";
jest.mock("../session", () => {
return {
Session: {
clear: jest.fn(),
getBool: jest.fn(),
}
};
});
import { maybeRefreshToken } from "../refresh_token";
import { API } from "../api/index";
import { Session } from "../session";
API.setBaseUrl("http://blah.whatever.party");
const fakeAuth = (jti = "456"): AuthState => ({
token: {
encoded: "---",
unencoded: {
iat: 123,
jti,
iss: "---",
exp: 456,
mqtt: "---",
os_update_server: "---"
}
}
});
describe("maybeRefreshToken()", () => {
it("gives you the old token when it cant find a new one", (done) => {
maybeRefreshToken(fakeAuth("111"))
.then((nextToken) => {
expect(nextToken.token.unencoded.jti).toEqual("111");
done();
});
it("logs you out when a refresh fails", (done) => {
const t = {
token: {
encoded: "---",
unencoded: {
iat: 123,
jti: "111",
iss: "---",
exp: 456,
mqtt: "---",
os_update_server: "---"
}
}
};
maybeRefreshToken(t).then(() => {
expect(Session.clear).toHaveBeenCalled();
done();
});
});
});

View File

@ -14,13 +14,12 @@ const mockAuth = (jti = "456"): AuthState => ({
jest.mock("axios", () => ({
default: {
get() {
return Promise.resolve({ data: mockAuth("000") });
},
interceptors: {
response: {
use: jest.fn()
},
get() {
return Promise.resolve({ data: mockAuth("000") });
}
response: { use: jest.fn() },
request: { use: jest.fn() }
}
}
}));

View File

@ -1,23 +1,45 @@
jest.unmock("../../auth/actions");
const actions = require("../../auth/actions");
const didLogin = jest.fn();
const mockState = {
auth: {
token: {
unencoded: { iss: "http://geocities.com" }
}
}
};
jest.mock("axios", () => ({
default: {
interceptors: {
response: { use: jest.fn() },
request: { use: jest.fn() }
},
get() { return Promise.resolve({ data: mockState }); }
}
}));
jest.mock("../../session", () => ({
Session: {
fetchStoredToken: jest.fn(),
getNum: () => undefined,
getBool: () => undefined,
getAll: () => undefined
}
}));
actions.didLogin = didLogin;
import { ready } from "../actions";
const STUB_STATE = { auth: "FOO BAR BAZ" };
jest.mock("../../auth/actions", () => ({
didLogin: jest.fn(),
setToken: jest.fn()
}));
import { ready } from "../actions";
import { setToken } from "../../auth/actions";
describe("Actions", () => {
it("fetches configs and calls didLogin()", () => {
it("calls didLogin()", () => {
jest.resetAllMocks();
const dispatch = jest.fn();
const getState = jest.fn(() => STUB_STATE);
const getState = jest.fn(() => mockState);
const thunk = ready();
thunk(dispatch, getState);
expect(didLogin.mock.calls.length).toBe(1);
expect(setToken).toHaveBeenCalled();
});
});

View File

@ -5,11 +5,10 @@ import { maybeRefreshToken } from "../refresh_token";
import { withTimeout } from "../util";
import { AuthState } from "../auth/interfaces";
export const storeToken =
(auth: AuthState, dispatch: Function) => () => {
dispatch(setToken(auth));
didLogin(auth, dispatch);
};
export const storeToken = (auth: AuthState, dispatch: Function) => () => {
dispatch(setToken(auth));
didLogin(auth, dispatch);
};
/** Amount of time we're willing to wait before concluding that the token is bad
* or the API is down. */

View File

@ -39,7 +39,7 @@ export namespace Session {
export function clear(): never {
localStorage.clear();
sessionStorage.clear();
window.location.href = window.location.origin;
// window.location.href = window.location.origin;
throw new Error("session cleared");
}

View File

@ -9,7 +9,6 @@ import { box } from "boxed_value";
import { TaggedResource } from "./resources/tagged_resources";
import { AxiosResponse } from "axios";
import { history } from "./history";
import { Session } from "./session";
// http://stackoverflow.com/a/901144/1064917
// Grab a query string param by name, because react-router-redux doesn't
@ -236,14 +235,14 @@ export function smoothScrollToBottom() {
let timer = 0;
if (stopY > startY) {
for (let i = startY; i < stopY; i += step) {
setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
setTimeout(() => window.scrollTo(0, leapY), timer * speed);
leapY += step;
if (leapY > stopY) { leapY = stopY; }
timer++;
} return;
}
for (let i = startY; i > stopY; i -= step) {
setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
setTimeout(() => window.scrollTo(0, leapY), timer * speed);
leapY -= step; if (leapY < stopY) { leapY = stopY; }
timer++;
}