Skip to content

Commit

Permalink
Add non-null constraint to device_id
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Nov 25, 2020
1 parent 89bac25 commit 79fb3cb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 33 deletions.
8 changes: 0 additions & 8 deletions app/controllers/api/abstract_controller.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/mutations/users/create.rb
Expand Up @@ -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
Expand Down
@@ -0,0 +1,5 @@
class AddNonNullConstraintToUsersDeviceId < ActiveRecord::Migration[6.0]
def change
change_column_null :users, :device_id, false
end
end
5 changes: 3 additions & 2 deletions db/structure.sql
Expand Up @@ -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,
Expand Down Expand Up @@ -3452,6 +3452,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200914165414'),
('20201105145245'),
('20201118183247'),
('20201120162403');
('20201120162403'),
('20201124235014');


25 changes: 8 additions & 17 deletions 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)
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/api/users/users_controller_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 79fb3cb

Please sign in to comment.