Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent unauthenticated users from signing up (#173)
* Prevent unauthenticated users from signing up * Disable cop complaining about rspec idiom Refer to rubocop/rubocop#4222 * Fix style violation * Change authorization mc -> admin following UserController
- Loading branch information
1 parent
7f7f194
commit 892e17d
Showing
4 changed files
with
118 additions
and
0 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
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,97 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe RegistrationsController, type: :controller do | ||
before do | ||
@request.env['devise.mapping'] = Devise.mappings[:user] | ||
end | ||
|
||
describe 'GET #new' do | ||
context 'unauthenticated' do | ||
it do | ||
get(:new) | ||
should redirect_to new_user_session_path | ||
end | ||
end | ||
context 'normal user' do | ||
it do | ||
sign_in create(:user) | ||
get :new | ||
should redirect_to users_path | ||
end | ||
end | ||
context 'mc' do | ||
it do | ||
sign_in create(:user, mc: true) | ||
get :new | ||
should redirect_to users_path | ||
end | ||
end | ||
context 'admin' do | ||
it do | ||
user = create(:user) | ||
user.add_role(:admin) | ||
sign_in user | ||
get :new | ||
should respond_with :ok | ||
end | ||
end | ||
end | ||
|
||
describe 'POST #create' do | ||
before do | ||
@params = { | ||
user: { | ||
username: 'Asd', matric_num: 'A101010J', contact_num: '85851212', | ||
email: 'asd@example.com', cell: 'marketing', mc: false, | ||
password: '123456', password_confirmation: '123456' | ||
} | ||
} | ||
end | ||
|
||
context 'unauthenticated' do | ||
it do | ||
expect { post :create, params: @params } | ||
.to_not change { User.count } | ||
should redirect_to new_user_session_path | ||
end | ||
end | ||
context 'normal user' do | ||
it do | ||
sign_in create(:user) | ||
expect { post :create, params: @params } | ||
.to_not change { User.count } | ||
should redirect_to users_path | ||
end | ||
end | ||
context 'mc' do | ||
it do | ||
sign_in create(:user, mc: true) | ||
expect { post :create, params: @params } | ||
.to_not change { User.count } | ||
should redirect_to users_path | ||
end | ||
end | ||
context 'admin' do | ||
it do | ||
user = create(:user) | ||
user.add_role(:admin) | ||
sign_in user | ||
expect { post :create, params: @params } | ||
.to change { User.count }.by(1) | ||
should redirect_to users_path | ||
end | ||
end | ||
end | ||
|
||
describe 'after_sign_up_path_for' do | ||
it { expect(subject.after_sign_up_path_for(nil)).to eq(users_path) } | ||
end | ||
|
||
describe 'after_inactive_sign_up_path_for' do | ||
it { | ||
expect(subject.after_inactive_sign_up_path_for(nil)).to eq(users_path) | ||
} | ||
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