2023-01-02 00:12:55 -08:00
# frozen_string_literal: true
require " rails_helper "
2022-01-01 12:58:36 -08:00
describe ServicesController , type : :controller do
2023-01-02 00:30:38 -08:00
describe " # index " do
subject { get :index }
context " user signed in " do
let ( :user ) { FactoryBot . create ( :user ) }
before { sign_in user }
it " renders the services settings page with no services " do
subject
expect ( response ) . to render_template ( " index " )
expect ( controller . instance_variable_get ( :@services ) ) . to be_empty
end
context " user has a service token expired notification " do
let ( :notification ) do
Notification :: ServiceTokenExpired . create (
target_id : user . id ,
target_type : " User::ExpiredTwitterServiceConnection " ,
recipient_id : user . id ,
new : true
)
end
it " marks the notification as read " do
expect { subject } . to change { notification . reload . new } . from ( true ) . to ( false )
end
end
context " user has Twitter connected " do
before do
Services :: Twitter . create ( user : , uid : 12 )
end
it " renders the services settings page " do
subject
expect ( response ) . to render_template ( " index " )
expect ( controller . instance_variable_get ( :@services ) ) . not_to be_empty
end
end
end
end
2023-01-02 00:15:51 -08:00
describe " # create " do
subject { get :create , params : { provider : " twitter " } }
2022-01-01 12:58:36 -08:00
2023-01-02 00:15:51 -08:00
context " successful Twitter sign in " do
let ( :user ) { FactoryBot . create ( :user ) }
before do
sign_in user
OmniAuth . config . test_mode = true
OmniAuth . config . mock_auth [ :twitter ] = OmniAuth :: AuthHash . new ( {
" provider " = > " twitter " ,
" uid " = > " 12 " ,
" info " = > { " nickname " = > " jack " } ,
" credentials " = > { " token " = > " AAAA " , " secret " = > " BBBB " }
} )
request . env [ " omniauth.auth " ] = OmniAuth . config . mock_auth [ :twitter ]
end
2022-01-01 12:58:36 -08:00
2023-01-02 00:15:51 -08:00
after do
OmniAuth . config . mock_auth [ :twitter ] = nil
end
2022-01-01 12:58:36 -08:00
2023-01-02 00:15:51 -08:00
context " no services connected " do
it " creates a service integration " do
expect { subject } . to change { Service . count } . by ( 1 )
end
2022-01-01 13:10:13 -08:00
end
2022-01-01 12:58:36 -08:00
2023-01-02 00:15:51 -08:00
context " a user has a service connected " do
let ( :other_user ) { FactoryBot . create ( :user ) }
let! ( :service ) { Services :: Twitter . create ( user : other_user , uid : 12 ) }
2022-01-01 12:58:36 -08:00
2023-01-02 00:15:51 -08:00
it " shows an error when trying to attach a service account which is already connected " do
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. " )
end
2022-01-01 12:58:36 -08:00
end
end
end
2022-01-04 16:00:18 -08:00
2023-01-02 00:15:51 -08:00
describe " # update " do
2023-01-02 00:12:55 -08:00
subject { patch :update , params : }
2022-01-04 16:00:18 -08:00
2023-01-02 00:12:55 -08:00
context " not signed in " do
2022-01-04 16:00:18 -08:00
let ( :params ) { { id : 1 } }
2023-01-02 00:12:55 -08:00
it " redirects to sign in page " do
2022-01-04 16:00:18 -08:00
subject
expect ( response ) . to redirect_to ( new_user_session_path )
end
end
2023-01-02 00:12:55 -08:00
context " user with Twitter connection " do
2022-01-04 16:00:18 -08:00
before { sign_in user }
let ( :user ) { FactoryBot . create ( :user ) }
2023-01-02 00:12:55 -08:00
let ( :service ) { Services :: Twitter . create ( user : , uid : 12 ) }
let ( :params ) { { id : service . id , service : { post_tag : } } }
2022-01-04 16:00:18 -08:00
2023-01-02 00:12:55 -08:00
context " tag is valid " do
let ( :post_tag ) { " # askaraccoon " }
2022-01-04 16:00:18 -08:00
2023-01-02 00:12:55 -08:00
it " updates a service connection " do
expect { subject } . to change { service . reload . post_tag } . to ( " # askaraccoon " )
2022-01-04 16:00:18 -08:00
expect ( response ) . to redirect_to ( services_path )
2022-01-27 13:32:03 -08:00
expect ( flash [ :success ] ) . to eq ( " Service updated successfully. " )
2022-01-04 16:00:18 -08:00
end
end
2023-01-02 00:12:55 -08:00
context " tag is too long " do
let ( :post_tag ) { " a " * 21 } # 1 character over the limit
2022-01-04 16:00:18 -08:00
2023-01-02 00:12:55 -08:00
it " shows an error " do
2022-01-04 16:00:18 -08:00
subject
expect ( response ) . to redirect_to ( services_path )
2022-01-27 13:32:03 -08:00
expect ( flash [ :error ] ) . to eq ( " Unable to update service. " )
2022-01-04 16:00:18 -08:00
end
end
end
end
2022-01-01 12:58:36 -08:00
end