Skip to content

Commit

Permalink
Iplement authentication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmiliano committed Aug 2, 2022
1 parent 23e8506 commit 1c56f35
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Ignore gemset file
.ruby-gemset
11 changes: 10 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class User < ApplicationRecord
:registerable,
jwt_revocation_strategy: JwtDenylist

# validates :username, uniqueness: { case_sensitive: false }, presence: true, allow_blank: false, format: { with: /\A[a-zA-Z0-9]+\z/ }
validates :username,
uniqueness: { case_sensitive: false },
presence: true,
allow_blank: false

validates :email,
format: { with: URI::MailTo::EMAIL_REGEXP },
uniqueness: {case_sentive: false },
presence: true,
allow_blank: false

end
9 changes: 8 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require 'faker'

FactoryBot.define do
factory :user do

id { 1 }
email { Faker::Internet.email }
password { Faker::Internet.password }
name { Faker::Name.name }
username { Faker::Internet.username }

end

factory :post do
Expand Down
16 changes: 15 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"

let(:user) { create(:user, password: password) }
let(:password) { 'swordfish123' }

context 'with invalid email' do
let(:email) { 'invalid_email' }

let(:user) { build(:user, email: email) }

it 'does not create user' do
expect(user.valid?).to be_falsy
end

end

end
36 changes: 10 additions & 26 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
Expand All @@ -32,8 +18,8 @@
puts e.to_s.strip
exit 1
end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
Expand All @@ -44,17 +30,6 @@
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
Expand All @@ -63,4 +38,13 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

# Devise
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Warden::Test::Helpers, type: :system
config.include Warden::Test::Helpers, type: :request
config.include Devise::Test::IntegrationHelpers, type: :system
config.include Devise::Test::IntegrationHelpers, type: :request

config.include FactoryBot::Syntax::Methods
end
11 changes: 11 additions & 0 deletions spec/requests/posts_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
require 'rails_helper'

RSpec.describe 'Posts', type: :request do

let(:user) { create(:user) }


describe 'GET /index' do
let!(:posts) { Array.new(10) { FactoryBot.create(:post) } }
let(:action) { get '/api/v1/posts' }

it 'returns all posts' do

sign_in user

action

expect(response).to have_http_status(:success)
Expand All @@ -29,6 +36,7 @@
}

it 'returns created post data' do
sign_in user
action

post_data = JSON.parse(response.body)
Expand All @@ -52,6 +60,7 @@
}

it 'returns an unprocessable_entity status' do
sign_in user
action

expect(response).to have_http_status(:unprocessable_entity)
Expand All @@ -70,6 +79,7 @@
}

it 'returns an unprocessable_entity status' do
sign_in user
action

expect(response).to have_http_status(:unprocessable_entity)
Expand All @@ -86,6 +96,7 @@
}

it 'returns status code 204' do
sign_in user
action

expect(response).to have_http_status(204)
Expand Down

0 comments on commit 1c56f35

Please sign in to comment.