-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove null constraint from name on signup (#44)
- Loading branch information
1 parent
d25561c
commit acf01a1
Showing
6 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
db/migrate/20230508205512_remove_name_null_constraint_from_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,6 @@ | |
let(:user) { User.first } | ||
describe 'missing parameter' do | ||
[ | ||
{ | ||
email: '[email protected]', | ||
password: 'password' | ||
}, | ||
{ | ||
email: '[email protected]', | ||
name: 'JoeBloggs' | ||
|
@@ -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 | ||
|