asciinema.org/spec/controllers/api_tokens_controller_spec.rb

61 lines
1.3 KiB
Ruby
Raw Normal View History

2014-08-30 17:38:47 +00:00
require 'rails_helper'
2012-03-04 19:30:26 +00:00
2014-02-09 13:44:09 +00:00
describe ApiTokensController do
2012-03-04 19:30:26 +00:00
describe '#create' do
subject { get :create, api_token: 'a-toh-can' }
2014-10-12 18:36:31 +00:00
let(:user) { double('user', assign_api_token: nil, username: 'foobar') }
before do
login_as(user)
end
2012-03-04 19:30:26 +00:00
context 'for guest user' do
let(:user) { nil }
before do
subject
end
2014-10-12 18:36:31 +00:00
it { should redirect_to(new_login_path) }
2014-10-12 18:36:31 +00:00
specify { expect(flash[:notice]).to match(/log in to proceed/) }
2012-03-04 19:30:26 +00:00
end
context "when assigning succeeds" do
before do
allow(user).to receive(:assign_api_token).with('a-toh-can')
subject
2012-03-04 19:30:26 +00:00
end
2014-10-12 18:36:31 +00:00
it { should redirect_to(public_profile_path(username: 'foobar')) }
specify { expect(flash[:notice]).to_not be_blank }
end
2012-03-04 19:30:26 +00:00
context "when token is invalid" do
before do
allow(user).to receive(:assign_api_token).with('a-toh-can').
and_raise(ActiveRecord::RecordInvalid, ApiToken.new)
end
it 'displays error page' do
expect(subject).to render_template(:error)
end
end
context "when token is taken" do
before do
allow(user).to receive(:assign_api_token).with('a-toh-can').
and_raise(ApiToken::ApiTokenTakenError)
end
2012-03-04 19:30:26 +00:00
it 'displays error page' do
expect(subject).to render_template(:error)
end
2012-03-04 19:30:26 +00:00
end
end
2012-03-04 19:30:26 +00:00
end