-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2394 from FarmBot/staging
v15.4.14
- Loading branch information
Showing
16 changed files
with
124 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
jest.mock("../session", () => ({ Session: { clear: jest.fn() } })); | ||
|
||
jest.mock("axios", () => ({ delete: jest.fn(() => Promise.resolve()) })); | ||
|
||
import axios from "axios"; | ||
import { API } from "../api"; | ||
import { logout } from "../logout"; | ||
import { Session } from "../session"; | ||
|
||
API.setBaseUrl(""); | ||
|
||
describe("logout()", () => { | ||
it("logs out", () => { | ||
logout()(); | ||
expect(Session.clear).toHaveBeenCalled(); | ||
expect(axios.delete).toHaveBeenCalledWith("http://localhost/api/tokens/"); | ||
}); | ||
|
||
it("keeps token", () => { | ||
logout(true)(); | ||
expect(Session.clear).toHaveBeenCalled(); | ||
expect(axios.delete).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import axios from "axios"; | ||
import { noop } from "lodash"; | ||
import { API } from "./api"; | ||
import { Session } from "./session"; | ||
|
||
export const logout = (keepToken = false) => () => { | ||
!keepToken && axios.delete(API.current.tokensPath).then(noop, noop); | ||
Session.clear(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
import React from "react"; | ||
import { mount, shallow } from "enzyme"; | ||
import { AdditionalMenu } from "../additional_menu"; | ||
import { AccountMenuProps } from "../interfaces"; | ||
|
||
describe("AdditionalMenu", () => { | ||
const fakeProps = (): AccountMenuProps => ({ | ||
isStaff: false, | ||
close: jest.fn(), | ||
}); | ||
|
||
it("renders the account menu", () => { | ||
const wrapper = mount(<AdditionalMenu | ||
logout={jest.fn()} | ||
close={jest.fn()} />); | ||
const wrapper = mount(<AdditionalMenu {...fakeProps()} />); | ||
const text = wrapper.text(); | ||
expect(text).toContain("Account Settings"); | ||
expect(text).toContain("Logout"); | ||
expect(text).toContain("VERSION"); | ||
expect(text).not.toContain("destroy token"); | ||
}); | ||
|
||
it("renders the account menu as staff", () => { | ||
const p = fakeProps(); | ||
p.isStaff = true; | ||
const wrapper = mount(<AdditionalMenu {...p} />); | ||
const text = wrapper.text(); | ||
expect(text).toContain("Account Settings"); | ||
expect(text).toContain("destroy token"); | ||
}); | ||
|
||
it("closes the account menu upon nav", () => { | ||
const close = jest.fn(); | ||
const wrapper = shallow(<AdditionalMenu | ||
logout={jest.fn()} | ||
close={(x) => () => close(x)} />); | ||
const p = fakeProps(); | ||
p.close = x => () => close(x); | ||
const wrapper = shallow(<AdditionalMenu {...p} />); | ||
wrapper.find("Link").first().simulate("click"); | ||
expect(close).toHaveBeenCalledWith("accountMenuOpen"); | ||
}); | ||
|
||
it("logs out", () => { | ||
const logout = jest.fn(); | ||
const wrapper = shallow(<AdditionalMenu | ||
logout={logout} | ||
close={jest.fn()} />); | ||
wrapper.find("a").at(0).simulate("click"); | ||
expect(logout).toHaveBeenCalled(); | ||
}); | ||
|
||
it("navigates to help page", () => { | ||
const wrapper = shallow(<AdditionalMenu | ||
logout={jest.fn()} | ||
close={jest.fn()} />); | ||
const wrapper = shallow(<AdditionalMenu {...fakeProps()} />); | ||
wrapper.find("Link").at(3).simulate("click"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
spec/controllers/api/tokens/tokens_controller_destroy_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "spec_helper" | ||
|
||
describe Api::TokensController do | ||
include Devise::Test::ControllerHelpers | ||
|
||
describe "#destroy" do | ||
let(:user) { FactoryBot.create(:user, password: "password") } | ||
let(:device) { FactoryBot.create(:device) } | ||
let(:auth_token) do | ||
SessionToken.issue_to(user, | ||
fbos_version: Gem::Version.new("9.9.9"), | ||
iat: Time.now.to_i, | ||
exp: 40.days.from_now.to_i) | ||
end | ||
|
||
it "destroys a token" do | ||
user.device = device | ||
request.headers["Authorization"] = "bearer #{auth_token.encoded}" | ||
delete :destroy | ||
expect(response.status).to eq(204) | ||
end | ||
|
||
it "denies bad tokens" do | ||
request.headers["Authorization"] = "bearer nope" | ||
delete :destroy | ||
expect(response.status).to eq(401) | ||
end | ||
end | ||
end |