Add login & user tests
This commit is contained in:
parent
7023600cd1
commit
26b8064553
@ -1,5 +1,5 @@
|
|||||||
class UserPolicy < ApplicationPolicy
|
class UserPolicy < ApplicationPolicy
|
||||||
def index
|
def index?
|
||||||
user.admin?
|
user.admin?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
5
spec/factories/contests.rb
Normal file
5
spec/factories/contests.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :contest do
|
||||||
|
name { Faker::Company.unique.name }
|
||||||
|
end
|
||||||
|
end
|
@ -1,7 +1,11 @@
|
|||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :user do
|
factory :user do
|
||||||
username { Faker::Internet.username }
|
username { Faker::Internet.unique.username }
|
||||||
email_address { Faker::Internet.email }
|
email_address { Faker::Internet.unique.email }
|
||||||
password { Faker::Internet.password(min_length: 12, max_length: 18) }
|
password { Faker::Internet.unique.password(min_length: 12, max_length: 18) }
|
||||||
|
end
|
||||||
|
|
||||||
|
trait :admin do
|
||||||
|
admin { true }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,10 +5,7 @@ RSpec.feature "Contests", type: :feature do
|
|||||||
let!(:user) { create(:user) }
|
let!(:user) { create(:user) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
visit '/'
|
login(user)
|
||||||
fill_in "Email address", with: user.email_address
|
|
||||||
fill_in "Password", with: user.password
|
|
||||||
click_button "Sign in"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should display the username" do
|
it "should display the username" do
|
||||||
|
34
spec/features/login_spec.rb
Normal file
34
spec/features/login_spec.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.feature "Login", type: :feature do
|
||||||
|
context "visiting the login page" do
|
||||||
|
let!(:user) { create(:user) }
|
||||||
|
|
||||||
|
it "should log in the user with the correct credentials" do
|
||||||
|
visit '/'
|
||||||
|
fill_in "Email address", with: user.email_address
|
||||||
|
fill_in "Password", with: user.password
|
||||||
|
click_button "Sign in"
|
||||||
|
|
||||||
|
expect(page).not_to have_content("Login")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fail to log in the user with an incorrect email address" do
|
||||||
|
visit '/'
|
||||||
|
fill_in "Email address", with: Faker::Internet.unique.email
|
||||||
|
fill_in "Password", with: user.password
|
||||||
|
click_button "Sign in"
|
||||||
|
|
||||||
|
expect(page).to have_content("Login")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should fail to log in the user with an incorrect password" do
|
||||||
|
visit '/'
|
||||||
|
fill_in "Email address", with: user.email_address
|
||||||
|
fill_in "Password", with: Faker::Internet.unique.password
|
||||||
|
click_button "Sign in"
|
||||||
|
|
||||||
|
expect(page).to have_content("Login")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
63
spec/features/user_spec.rb
Normal file
63
spec/features/user_spec.rb
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.feature "Users", type: :feature do
|
||||||
|
context "when the user is a regular user" do
|
||||||
|
let!(:user) { create(:user) }
|
||||||
|
let!(:contest) { create(:contest, user: user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
login(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not see a link to all users" do
|
||||||
|
visit root_path
|
||||||
|
|
||||||
|
expect(page).not_to have_content("Users")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not be able to see the user list" do
|
||||||
|
visit users_path
|
||||||
|
|
||||||
|
expect(page).not_to have_content("All users")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to create a new contest" do
|
||||||
|
visit root_path
|
||||||
|
|
||||||
|
click_link "Create a new contest"
|
||||||
|
|
||||||
|
expect(page).to have_content("New jigsaw puzzle competition")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to open an existing contest" do
|
||||||
|
visit root_path
|
||||||
|
|
||||||
|
expect(page).to have_content(contest.name)
|
||||||
|
find("div.card", text: contest.name).find("a").click()
|
||||||
|
|
||||||
|
expect(page).to have_content("Edit contest")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the user is an admin" do
|
||||||
|
let!(:admin) { create(:user, :admin) }
|
||||||
|
let!(:user) { create(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
login(admin)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should see a link to all users" do
|
||||||
|
visit root_path
|
||||||
|
|
||||||
|
expect(page).to have_content("Users")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to see the user list" do
|
||||||
|
visit users_path
|
||||||
|
|
||||||
|
expect(page).to have_content("All users")
|
||||||
|
expect(page).to have_content(user.username)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
12
spec/support/helpers.rb
Normal file
12
spec/support/helpers.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module Helpers
|
||||||
|
def login(user)
|
||||||
|
visit '/'
|
||||||
|
fill_in "Email address", with: user.email_address
|
||||||
|
fill_in "Password", with: user.password
|
||||||
|
click_button "Sign in"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec.configure do |c|
|
||||||
|
c.include Helpers
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user