Skip to content

Commit

Permalink
Remove null constraint from name on signup (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorkshireman authored May 8, 2023
1 parent d25561c commit acf01a1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def signin
end

def signup
User.new(user_params(true)).then do |user|
User.new(user_params).then do |user|
render_success_response(201, user.id) if user.save!
rescue ActiveRecord::RecordInvalid => e
render_error_response(403, e)
Expand Down
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, uniqueness: true
validates :name, presence: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveNameNullConstraintFromUsers < ActiveRecord::Migration[6.0]
def change
change_column_null :users, :name, true
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_06_231715) do
ActiveRecord::Schema.define(version: 2023_05_08_205512) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"

create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "email", null: false
t.string "name", null: false
t.string "name"
t.string "password_digest", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
expect(user.password).to eq('password')
end

it 'cannot be created without a name' do
expect { User.create(email: '[email protected]', password: 'password') }.to change { User.count }.by(0)
it 'can be created without a name' do
expect { User.create(email: '[email protected]', password: 'password') }.to change { User.count }.by(1)
end

it 'cannot be created without an email' do
Expand Down
70 changes: 66 additions & 4 deletions spec/requests/signup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
let(:user) { User.first }
describe 'missing parameter' do
[
{
email: '[email protected]',
password: 'password'
},
{
email: '[email protected]',
name: 'JoeBloggs'
Expand Down Expand Up @@ -124,6 +120,72 @@
end
end

describe 'when request is valid and missing name' do
let(:user) { User.first }
include ActiveSupport::Testing::TimeHelpers
before :all do
# thought there wouldn't be a need for this. Why doesn't the test db reset after each run of the suite?
User.destroy_all
headers = {
'CONTENT_TYPE' => 'application/vnd.api+json'
}

params = JSON.generate({
user: {
email: '[email protected]',
password: 'password'
}
})

freeze_time do
@time_now = Time.now
post '/signup', headers: headers, params: params
end
end

describe 'user' do
it 'user has correct email' do
expect(user.email).to eq('[email protected]')
end

it 'user has no name' do
expect(user.name).to be nil
end

it 'user has a password' do
expect(user.password_digest).to be_truthy
end
end

describe 'response' do
it 'has correct Content-Type header value' do
expect(response.content_type).to eq('application/vnd.api+json')
end

it 'has 201 status code' do
expect(response).to have_http_status(201)
end

describe 'token' do
it 'is a String' do
expect(JSON.parse(response.body)['data']['token']).to be_a(String)
end

it 'contains correct information' do
expected_decoded_token = [{ 'exp' => (@time_now + 1800).to_i, 'user_id' => user.id }, { 'alg' => 'HS256' }]
actual_decoded_token = JWT.decode(
JSON.parse(response.body)['data']['token'],
ENV['JWT_SECRET_KEY'],
true,
{ algorithm: 'HS256' }
)

expect(actual_decoded_token).to eq(expected_decoded_token)
end
end
end
end

describe 'when attempting to create a user with a duplicate email' do
before :all do
User.destroy_all
Expand Down

0 comments on commit acf01a1

Please sign in to comment.