Reformat services controller spec

This commit is contained in:
Karina Kwiatek 2023-01-02 09:12:55 +01:00
parent 02bcfb3c9e
commit 0f01177c67
1 changed files with 27 additions and 25 deletions

View File

@ -1,17 +1,19 @@
require 'rails_helper' # frozen_string_literal: true
require "rails_helper"
describe ServicesController, type: :controller do describe ServicesController, type: :controller do
context 'successful Twitter sign in' do context "successful Twitter sign in" do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
before do before do
sign_in user sign_in user
OmniAuth.config.test_mode = true OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({ OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
'provider' => 'twitter', "provider" => "twitter",
'uid' => '12', "uid" => "12",
'info' => { 'nickname' => 'jack' }, "info" => { "nickname" => "jack" },
'credentials' => { 'token' => 'AAAA', 'secret' => 'BBBB' } "credentials" => { "token" => "AAAA", "secret" => "BBBB" }
}) })
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end end
@ -20,58 +22,58 @@ describe ServicesController, type: :controller do
OmniAuth.config.mock_auth[:twitter] = nil OmniAuth.config.mock_auth[:twitter] = nil
end end
subject { get :create, params: { provider: 'twitter' } } subject { get :create, params: { provider: "twitter" } }
context 'no services connected' do context "no services connected" do
it 'creates a service integration' do it "creates a service integration" do
expect { subject }.to change { Service.count }.by(1) expect { subject }.to change { Service.count }.by(1)
end end
end end
context 'a user has a service connected' do context "a user has a service connected" do
let(:other_user) { FactoryBot.create(:user) } let(:other_user) { FactoryBot.create(:user) }
let!(:service) { Services::Twitter.create(user: other_user, uid: 12) } let!(:service) { Services::Twitter.create(user: other_user, uid: 12) }
it 'shows an error when trying to attach a service account which is already connected' do it "shows an error when trying to attach a service account which is already connected" do
subject subject
expect(flash[:error]).to eq("The Twitter account you are trying to connect is already connected to another #{APP_CONFIG['site_name']} account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring.") expect(flash[:error]).to eq("The Twitter account you are trying to connect is already connected to another #{APP_CONFIG['site_name']} account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring.")
end end
end end
end end
context '#update' do context "#update" do
subject { patch :update, params: params } subject { patch :update, params: }
context 'not signed in' do context "not signed in" do
let(:params) { { id: 1 } } let(:params) { { id: 1 } }
it 'redirects to sign in page' do it "redirects to sign in page" do
subject subject
expect(response).to redirect_to(new_user_session_path) expect(response).to redirect_to(new_user_session_path)
end end
end end
context 'user with Twitter connection' do context "user with Twitter connection" do
before { sign_in user } before { sign_in user }
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:service) { Services::Twitter.create(user: user, uid: 12) } let(:service) { Services::Twitter.create(user:, uid: 12) }
let(:params) { { id: service.id, service: { post_tag: post_tag } } } let(:params) { { id: service.id, service: { post_tag: } } }
context 'tag is valid' do context "tag is valid" do
let(:post_tag) { '#askaraccoon' } let(:post_tag) { "#askaraccoon" }
it 'updates a service connection' do it "updates a service connection" do
expect { subject }.to change { service.reload.post_tag }.to('#askaraccoon') expect { subject }.to change { service.reload.post_tag }.to("#askaraccoon")
expect(response).to redirect_to(services_path) expect(response).to redirect_to(services_path)
expect(flash[:success]).to eq("Service updated successfully.") expect(flash[:success]).to eq("Service updated successfully.")
end end
end end
context 'tag is too long' do context "tag is too long" do
let(:post_tag) { 'a' * 21 } # 1 character over the limit let(:post_tag) { "a" * 21 } # 1 character over the limit
it 'shows an error' do it "shows an error" do
subject subject
expect(response).to redirect_to(services_path) expect(response).to redirect_to(services_path)
expect(flash[:error]).to eq("Unable to update service.") expect(flash[:error]).to eq("Unable to update service.")