diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb deleted file mode 100644 index deaa39a0..00000000 --- a/app/controllers/services_controller.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -class ServicesController < ApplicationController - before_action :authenticate_user! - before_action :mark_notifications_as_read, only: %i[index] - - def index - @services = current_user.services - end - - def create - service = Service.initialize_from_omniauth(omniauth_hash) - service.user = current_user - service_name = service.type.split("::").last.titleize - - if service.save - flash[:success] = t(".success", service: service_name) - else - flash[:error] = if service.errors.details[:uid]&.any? { |err| err[:error] == :taken } - t(".duplicate", service: service_name, app: APP_CONFIG["site_name"]) - else - t(".error", service: service_name) - end - end - - redirect_to origin || services_path - end - - def update - service = current_user.services.find(params[:id]) - service.post_tag = params[:service][:post_tag].tr("@", "") - if service.save - flash[:success] = t(".success") - else - flash[:error] = t(".error") - end - redirect_to services_path - end - - def failure - Rails.logger.info "oauth error: #{params.inspect}" - flash[:error] = t(".error") - redirect_to services_path - end - - def destroy - @service = current_user.services.find(params[:id]) - service_name = @service.type.split("::").last.titleize - @service.destroy - flash[:success] = t(".success", service: service_name) - redirect_to services_path - end - - private - - def origin - request.env["omniauth.origin"] - end - - def omniauth_hash - request.env["omniauth.auth"] - end - - def mark_notifications_as_read - Notification::ServiceTokenExpired - .where(recipient: current_user, new: true) - .update_all(new: false) # rubocop:disable Rails/SkipsModelValidations - end -end diff --git a/config/locales/controllers.en.yml b/config/locales/controllers.en.yml index 1373b154..7c047680 100644 --- a/config/locales/controllers.en.yml +++ b/config/locales/controllers.en.yml @@ -148,18 +148,6 @@ en: author: info: "No questions from @%{author} found, showing entries from all users instead!" error: "No user with the name @%{author} found, showing entries from all users instead!" - services: - create: - success: "%{service} connected successfully." - duplicate: "The %{service} account you are trying to connect is already connected to another %{app} account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring." - error: "Unable to connect to %{service}." - update: - success: "Service updated successfully." - error: "Unable to update service." - failure: - error: :errors.base - destroy: - success: "%{service} disconnected successfully." settings: export: index: diff --git a/config/routes.rb b/config/routes.rb index 7dca9c1a..fbc7aa0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -101,17 +101,6 @@ Rails.application.routes.draw do resolve("Theme") { [:settings_theme] } # to make link_to/form_for work nicely when passing a `Theme` object to it, see also: https://api.rubyonrails.org/v6.1.5.1/classes/ActionDispatch/Routing/Mapper/CustomUrls.html#method-i-resolve resolve("Profile") { [:settings_profile] } - # resources :services, only: [:index, :destroy] - get "/settings/services", to: "services#index", as: :services - patch "/settings/services/:id", to: "services#update", as: :update_service - delete "/settings/services/:id", to: "services#destroy", as: :service - controller :services do - scope "/auth", as: "auth" do - get ":provider/callback" => :create - get :failure - end - end - namespace :ajax do post "/ask", to: "question#create", as: :ask post "/destroy_question", to: "question#destroy", as: :destroy_question diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb deleted file mode 100644 index 5e9582ab..00000000 --- a/spec/controllers/services_controller_spec.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -describe ServicesController, type: :controller do - 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 - - describe "#create" do - subject { get :create, params: { provider: "twitter" } } - - 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 - - after do - OmniAuth.config.mock_auth[:twitter] = nil - end - - context "no services connected" do - it "creates a service integration" do - expect { subject }.to change { Service.count }.by(1) - end - end - - context "a user has a service connected" do - let(:other_user) { FactoryBot.create(:user) } - 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 - 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 - end - end - end - - describe "#update" do - subject { patch :update, params: } - - context "not signed in" do - let(:params) { { id: 1 } } - - it "redirects to sign in page" do - subject - expect(response).to redirect_to(new_user_session_path) - end - end - - context "user with Twitter connection" do - before { sign_in user } - - let(:user) { FactoryBot.create(:user) } - let(:service) { Services::Twitter.create(user:, uid: 12) } - let(:params) { { id: service.id, service: { post_tag: } } } - - context "tag is valid" do - let(:post_tag) { "#askaraccoon" } - - it "updates a service connection" do - expect { subject }.to change { service.reload.post_tag }.to("#askaraccoon") - expect(response).to redirect_to(services_path) - expect(flash[:success]).to eq("Service updated successfully.") - end - end - - context "tag is too long" do - let(:post_tag) { "a" * 21 } # 1 character over the limit - - it "shows an error" do - subject - expect(response).to redirect_to(services_path) - expect(flash[:error]).to eq("Unable to update service.") - end - end - end - end -end