diff --git a/app/controllers/api/abstract_controller.rb b/app/controllers/api/abstract_controller.rb index d3ba806933..c03ab3913e 100644 --- a/app/controllers/api/abstract_controller.rb +++ b/app/controllers/api/abstract_controller.rb @@ -51,10 +51,6 @@ class OnlyJson < Exception; end sorry ONLY_JSON end - rescue_from Errors::NoBot do |exc| - sorry "You need to register a device first." - end - rescue_from ActiveRecord::RecordNotFound do |exc| sorry "Document not found.", 404 end @@ -178,10 +174,6 @@ def skip_set_cookies_header reset_session end - def no_device - raise Errors::NoBot - end - def authenticate_user! # All possible information that could be needed for any of the 3 auth # strategies. diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 24eeaafe8a..833140d30b 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,7 +10,7 @@ def unset_current_device def current_device return @current_device if @current_device authenticate_user! unless current_user - @current_device = current_user&.device || no_device + @current_device = current_user.device Device.send(:current=, @current_device) @current_device end diff --git a/app/mutations/users/create.rb b/app/mutations/users/create.rb index 3518b4a9cd..a49bbf6052 100644 --- a/app/mutations/users/create.rb +++ b/app/mutations/users/create.rb @@ -34,8 +34,9 @@ def execute password_confirmation: password_confirmation, name: name } params[:agreed_to_terms_at] = Time.now - user = User.create!(params) + user = User.new(params) device = Devices::Create.run!(user: user) + user.save! UserMailer .welcome_email(user) .deliver_later unless skip_email diff --git a/db/migrate/20201124235014_add_non_null_constraint_to_users_device_id.rb b/db/migrate/20201124235014_add_non_null_constraint_to_users_device_id.rb new file mode 100644 index 0000000000..c71c1b12d1 --- /dev/null +++ b/db/migrate/20201124235014_add_non_null_constraint_to_users_device_id.rb @@ -0,0 +1,5 @@ +class AddNonNullConstraintToUsersDeviceId < ActiveRecord::Migration[6.0] + def change + change_column_null :users, :device_id, false + end +end diff --git a/db/structure.sql b/db/structure.sql index 4594707beb..0319d787be 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1662,7 +1662,7 @@ ALTER SEQUENCE public.tools_id_seq OWNED BY public.tools.id; CREATE TABLE public.users ( id integer NOT NULL, - device_id integer, + device_id integer NOT NULL, name character varying, email character varying(280) DEFAULT ''::character varying NOT NULL, encrypted_password character varying DEFAULT ''::character varying NOT NULL, @@ -3452,6 +3452,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200914165414'), ('20201105145245'), ('20201118183247'), -('20201120162403'); +('20201120162403'), +('20201124235014'); diff --git a/spec/controllers/api/devices/devices_controller_show_spec.rb b/spec/controllers/api/devices/devices_controller_show_spec.rb index 6ed5e25c17..cf6872823b 100644 --- a/spec/controllers/api/devices/devices_controller_show_spec.rb +++ b/spec/controllers/api/devices/devices_controller_show_spec.rb @@ -1,30 +1,21 @@ -require 'spec_helper' +require "spec_helper" describe Api::DevicesController do - include Devise::Test::ControllerHelpers let(:user) { FactoryBot.create(:user) } - describe '#show' do - it 'handles deviceless requests' do - user.update(device: nil) - sign_in user - get :show, params: {}, session: { format: :json } - expect(response.status).to eq(422) - expect(json[:error]).to include("You need to register a device first.") - end - - it 'has expected keys' do + describe "#show" do + it "has expected keys" do sign_in user get :show, params: {}, session: { format: :json } - { id: Integer, - name: String }.each do |name, klass| - expect(json[name]).to be_an_instance_of(klass) - end + { id: Integer, + name: String }.each do |name, klass| + expect(json[name]).to be_an_instance_of(klass) + end end - it 'reminds users to agree to TOS' do + it "reminds users to agree to TOS" do b4 = User::ENFORCE_TOS const_reassign(User, :ENFORCE_TOS, "http://farm.bot/tos") user.update!(agreed_to_terms_at: nil) diff --git a/spec/controllers/api/users/users_controller_spec.rb b/spec/controllers/api/users/users_controller_spec.rb index 1c6e3bcda3..65684ea825 100644 --- a/spec/controllers/api/users/users_controller_spec.rb +++ b/spec/controllers/api/users/users_controller_spec.rb @@ -185,8 +185,8 @@ verified = User.create!(email: Faker::Internet.email, password: "password123", password_confirmation: "password123", - confirmed_at: Time.now) - + confirmed_at: Time.now, + device: FactoryBot.create(:device)) post :resend_verification, body: { email: verified.email }.to_json, format: :json @@ -198,8 +198,8 @@ it "re-sends verification email" do unverified = User.create!(email: Faker::Internet.email, password: "password123", - password_confirmation: "password123") - + password_confirmation: "password123", + device: FactoryBot.create(:device)) post :resend_verification, body: { email: unverified.email }.to_json, format: :json