-
Notifications
You must be signed in to change notification settings - Fork 1
Add User Model #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
809d45a
Add a concern for EmailFormat
MarkOates 2d3a219
Add User model and migration
MarkOates 740df2c
Add pry for debugging and testing
MarkOates 2af75c2
Add validations for :username and :user in User
MarkOates 6955784
Prevent duplicate username and email in User
MarkOates 5fb4210
Save user emails in lowercase
MarkOates 517b8df
Add bcrypt for user passwords
MarkOates 08c0dd5
Add secure password to Users model
MarkOates 7f3e2d7
Add PasswordFormat concern
MarkOates f67ba54
Require passwords to have 8 chars, digit, no trailing/ending whitespace
MarkOates File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| module EmailFormat | ||
| EMAIL = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i | ||
| end |
This file contains hidden or 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,6 @@ | ||
| module PasswordFormat | ||
| EIGHT_OR_MORE_CHARACTERS = /\A.{8,}\z/ | ||
| CONTAINS_A_DIGIT = /\d/ | ||
| STARTS_WITH_NON_WHITESPACE = /\A\S/ | ||
| ENDS_WITH_NON_WHITESPACE = /\S\z/ | ||
| end |
This file contains hidden or 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,21 @@ | ||
| class User < ApplicationRecord | ||
| before_save { email.downcase! } | ||
| validates :username, | ||
| presence: true, | ||
| length: { maximum: 24 }, | ||
| uniqueness: { case_sensitive: false } | ||
| validates :email, | ||
| presence: true, | ||
| length: { maximum: 255 }, | ||
| format: { with: EmailFormat::EMAIL }, | ||
| uniqueness: { case_sensitive: false } | ||
| has_secure_password | ||
| validates :password, format: { with: PasswordFormat::EIGHT_OR_MORE_CHARACTERS, | ||
| message: 'must have 8 or more characters' } | ||
| validates :password, format: { with: PasswordFormat::CONTAINS_A_DIGIT, | ||
| message: 'must have at least one digit' } | ||
| validates :password, format: { with: PasswordFormat::STARTS_WITH_NON_WHITESPACE, | ||
| message: 'can not start with whitespace' } | ||
| validates :password, format: { with: PasswordFormat::ENDS_WITH_NON_WHITESPACE, | ||
| message: 'can not end with whitespace' } | ||
| end |
This file contains hidden or 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,14 @@ | ||
| class CreateUsers < ActiveRecord::Migration[5.0] | ||
| def change | ||
| create_table :users do |t| | ||
| t.string :username | ||
| t.string :email | ||
| t.string :first_name | ||
| t.string :last_name | ||
| t.string :website | ||
| t.text :description | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 AddPasswordDigestToUsers < ActiveRecord::Migration[5.0] | ||
| def change | ||
| add_column :users, :password_digest, :string | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,7 @@ | ||
| markoates: | ||
| username: markoates | ||
| email: marks@example.com | ||
| first_name: Mark | ||
| last_name: Oates | ||
| website: www.example.com | ||
| description: I like cats! |
This file contains hidden or 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,84 @@ | ||
| require 'test_helper' | ||
|
|
||
| class UserTest < ActiveSupport::TestCase | ||
| test 'creates a new user in the database' do | ||
| User.destroy_all | ||
| new_user = User.create!(username: 'mr. test', | ||
| email: 'test@email.com', | ||
| password: 'pass1word', | ||
| password_confirmation: 'pass1word' | ||
| ) | ||
| assert_equal User.count, 1 | ||
| end | ||
|
|
||
| test 'username must be present' do | ||
| new_user = User.create | ||
| assert_includes new_user.errors[:username], "can't be blank" | ||
| end | ||
|
|
||
| test 'username can not be longer than 24 characters' do | ||
| too_long_username = 'a' * 25 | ||
| new_user = User.create(username: too_long_username) | ||
| assert_includes new_user.errors[:email], 'is invalid' | ||
| end | ||
|
|
||
| test 'username must be unique' do | ||
| already_existing_username = users(:markoates).username | ||
| new_user = User.create(username: already_existing_username) | ||
| assert_includes new_user.errors[:username], 'has already been taken' | ||
| end | ||
|
|
||
| test 'email must be present' do | ||
| new_user = User.create | ||
| assert_includes new_user.errors[:email], "can't be blank" | ||
| end | ||
|
|
||
| test 'email must be less that 255 characters' do | ||
| too_long_email = 'a' * 256 | ||
| new_user = User.create(email: too_long_email) | ||
| assert_includes new_user.errors[:email], 'is invalid' | ||
| end | ||
|
|
||
| test 'email must be valid' do | ||
| new_user = User.create(email: 'an_invalid%^&*email') | ||
| assert_includes new_user.errors[:email], 'is invalid' | ||
| end | ||
|
|
||
| test 'email must be unique' do | ||
| already_existing_email = users(:markoates).email | ||
| new_user = User.create(email: already_existing_email) | ||
| assert_includes new_user.errors[:email], 'has already been taken' | ||
| end | ||
|
|
||
| test 'email is saved in lowercase' do | ||
| jumblecase_email = 'JuMbLeCaSe@EmAiL.CoM' | ||
| new_user = User.create(username: 'Mrs. Jumble', | ||
| email: jumblecase_email, | ||
| password: 'pass1word', | ||
| password_confirmation: 'pass1word' | ||
| ) | ||
| new_user.save | ||
| new_user.reload | ||
| assert_includes new_user.email, 'jumblecase@email.com' | ||
| end | ||
|
|
||
| test 'with a password less than 8 characters, is invalid' do | ||
| new_user = User.create(password: 'pw2shrt') | ||
| assert_includes new_user.errors[:password], 'must have 8 or more characters' | ||
| end | ||
|
|
||
| test 'with a password that does not contain at least one digit, is invalid' do | ||
| new_user = User.create(password: 'nodigitshere') | ||
| assert_includes new_user.errors[:password], 'must have at least one digit' | ||
| end | ||
|
|
||
| test 'with a password that starts with whitespace, is invalid' do | ||
| new_user = User.create(password: ' startwithspace') | ||
| assert_includes new_user.errors[:password], 'can not start with whitespace' | ||
| end | ||
|
|
||
| test 'with a password that ends with whitespace, is invalid' do | ||
| new_user = User.create(password: 'endswithspace ') | ||
| assert_includes new_user.errors[:password], 'can not end with whitespace' | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be