Add login & user tests
All checks were successful
CI / scan_ruby (push) Successful in 15s
CI / scan_js (push) Successful in 12s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 35s

This commit is contained in:
sto 2025-03-27 10:26:03 +01:00
parent 7023600cd1
commit 26b8064553
7 changed files with 123 additions and 8 deletions

View File

@ -1,5 +1,5 @@
class UserPolicy < ApplicationPolicy class UserPolicy < ApplicationPolicy
def index def index?
user.admin? user.admin?
end end

View File

@ -0,0 +1,5 @@
FactoryBot.define do
factory :contest do
name { Faker::Company.unique.name }
end
end

View File

@ -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

View File

@ -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

View 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

View 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
View 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