Tests.
parent
0a84884ab5
commit
00fa4c82f0
|
@ -0,0 +1,39 @@
|
|||
jest.mock("axios", () => ({
|
||||
default: {
|
||||
get() {
|
||||
return Promise.reject("NO");
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
import { AuthState } from "../auth/interfaces";
|
||||
import { maybeRefreshToken } from "../refresh_token";
|
||||
import { API } from "../api/index";
|
||||
|
||||
API.setBaseUrl("http://blah.whatever.party");
|
||||
|
||||
const fakeAuth = (jti = "456"): AuthState => ({
|
||||
token: {
|
||||
encoded: "---",
|
||||
unencoded: {
|
||||
iat: 123,
|
||||
jti,
|
||||
iss: "---",
|
||||
exp: 456,
|
||||
mqtt: "---",
|
||||
os_update_server: "---",
|
||||
fw_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();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,38 @@
|
|||
const mockAuth = (jti = "456"): AuthState => ({
|
||||
token: {
|
||||
encoded: "---",
|
||||
unencoded: {
|
||||
iat: 123,
|
||||
jti,
|
||||
iss: "---",
|
||||
exp: 456,
|
||||
mqtt: "---",
|
||||
os_update_server: "---",
|
||||
fw_update_server: "---"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
jest.mock("axios", () => ({
|
||||
default: {
|
||||
get() {
|
||||
return Promise.resolve({ data: mockAuth("000") });
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
import { AuthState } from "../auth/interfaces";
|
||||
import { maybeRefreshToken } from "../refresh_token";
|
||||
import { API } from "../api/index";
|
||||
|
||||
API.setBaseUrl("http://whateer.party");
|
||||
|
||||
describe("maybeRefreshToken()", () => {
|
||||
it("gives you back your token when things fail", (done) => {
|
||||
maybeRefreshToken(mockAuth("111"))
|
||||
.then((nextToken) => {
|
||||
expect(nextToken.token.unencoded.jti).toEqual("000");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -3,17 +3,6 @@ import { API } from "./api/index";
|
|||
import { HttpData } from "./util";
|
||||
import { AuthState } from "./auth/interfaces";
|
||||
|
||||
export function maybeRefreshToken(old: AuthState) {
|
||||
|
||||
const no = () => {
|
||||
console.log("Can't DL new token. Here's the old one:");
|
||||
return old;
|
||||
};
|
||||
|
||||
const ok = (resp: HttpData<AuthState>) => {
|
||||
console.log("Here's a fresh token");
|
||||
return resp.data;
|
||||
};
|
||||
|
||||
return axios.get(API.current.tokensPath).then(ok, no);
|
||||
}
|
||||
export let maybeRefreshToken = (old: AuthState) => axios
|
||||
.get(API.current.tokensPath)
|
||||
.then(x => x.data, () => (old));
|
||||
|
|
Loading…
Reference in New Issue