personal_access_tokens_spec.rb 3.3 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Profile > Personal Access Tokens', feature: true, js: true do
4 5
  let(:user) { create(:user) }

6
  def active_personal_access_tokens
7
    find(".table.active-personal-access-tokens")
8 9 10
  end

  def inactive_personal_access_tokens
11
    find(".table.inactive-personal-access-tokens")
12 13 14
  end

  def created_personal_access_token
15
    find("#created-personal-access-token").value
16 17
  end

18 19 20 21 22 23
  def disallow_personal_access_token_saves!
    allow_any_instance_of(PersonalAccessToken).to receive(:save).and_return(false)
    errors = ActiveModel::Errors.new(PersonalAccessToken.new).tap { |e| e.add(:name, "cannot be nil") }
    allow_any_instance_of(PersonalAccessToken).to receive(:errors).and_return(errors)
  end

24 25 26 27 28
  before do
    login_as(user)
  end

  describe "token creation" do
29
    it "allows creation of a token" do
30 31 32
      visit profile_personal_access_tokens_path
      fill_in "Name", with: FFaker::Product.brand

T
Timothy Andrew 已提交
33 34
      expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
      expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
35 36
      expect(active_personal_access_tokens).to have_text(PersonalAccessToken.last.name)
      expect(active_personal_access_tokens).to have_text("Never")
37
    end
38

39 40
    it "allows creation of a token with an expiry date" do
      visit profile_personal_access_tokens_path
41
      fill_in "Name", with: FFaker::Product.brand
42 43

      # Set date to 1st of next month
T
Timothy Andrew 已提交
44
      find_field("Expires at").trigger('focus')
45 46 47
      find("a[title='Next']").click
      click_on "1"

T
Timothy Andrew 已提交
48 49
      expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
      expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
50 51
      expect(active_personal_access_tokens).to have_text(PersonalAccessToken.last.name)
      expect(active_personal_access_tokens).to have_text(Date.today.next_month.at_beginning_of_month.to_s(:medium))
52
    end
53 54 55 56 57 58 59

    context "when creation fails" do
      it "displays an error message" do
        disallow_personal_access_token_saves!
        visit profile_personal_access_tokens_path
        fill_in "Name", with: FFaker::Product.brand

T
Timothy Andrew 已提交
60
        expect { click_on "Create Personal Access Token" }.not_to change { PersonalAccessToken.count }
61 62 63
        expect(page).to have_content("Name cannot be nil")
      end
    end
64 65 66
  end

  describe "inactive tokens" do
67 68
    let!(:personal_access_token) { create(:personal_access_token, user: user) }

69 70 71 72
    it "allows revocation of an active token" do
      visit profile_personal_access_tokens_path
      click_on "Revoke"

73
      expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
74 75 76
    end

    it "moves expired tokens to the 'inactive' section" do
77
      personal_access_token.update(expires_at: 5.days.ago)
78 79
      visit profile_personal_access_tokens_path

80
      expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
81
    end
82 83 84 85 86 87 88 89 90 91 92

    context "when revocation fails" do
      it "displays an error message" do
        disallow_personal_access_token_saves!
        visit profile_personal_access_tokens_path

        expect { click_on "Revoke" }.not_to change { PersonalAccessToken.inactive.count }
        expect(active_personal_access_tokens).to have_text(personal_access_token.name)
        expect(page).to have_content("Could not revoke")
      end
    end
93 94
  end
end