Created
September 16, 2014 18:27
-
-
Save vasilakisfil/48685e85e991b70a5241 to your computer and use it in GitHub Desktop.
rspec tests
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
require 'rails_helper' | |
describe Api::V1::UsersController, type: :api do | |
context :index do | |
before do | |
5.times{ FactoryGirl.create(:user) } | |
5.times{ FactoryGirl.create(:admin) } | |
FactoryGirl.create(:super_admin) | |
sign_in(User.regular.last) | |
end | |
context 'with regular user' do | |
before do | |
get api_v1_users_path, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the correct number of data in the body' do | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:users].length).to eql(1) | |
end | |
end | |
context 'with admin user' do | |
before do | |
get api_v1_users_path, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the correct number of data in the body' do | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:users].length).to eql(1) | |
end | |
end | |
context 'with admin user that manages a unit with users' do | |
before do | |
admin = User.admin.last | |
FactoryGirl.create( | |
:unit, | |
user_ids: User.regular.limit(3).ids.concat([admin.id]) | |
) | |
sign_in(admin) | |
get api_v1_users_path, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the correct number of data in the body' do | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:users].length).to eql(3 + 1) | |
end | |
end | |
context 'with super_admin' do | |
before do | |
sign_in(User.super_admin.last) | |
get api_v1_users_path, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the correct number of data in the body' do | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:users].length).to eql(5+5+1) | |
end | |
end | |
end | |
context :resetpassword do | |
before do | |
@user = FactoryGirl.create(:user) | |
@user_json = @user.as_json(only: [:email]) | |
post resetpassword_api_v1_users_path(@user.id), user: @user_json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(202) | |
end | |
it "sets user's reset_password_token" do | |
@user.reload | |
expect(@user.reset_password_token).not_to eql(nil) | |
expect(@user.reset_password_sent_at.to_i).to eql(DateTime.now.to_i) | |
end | |
end | |
context :create do | |
before do | |
user = FactoryGirl.attributes_for(:user) | |
post api_v1_users_path, user: user.as_json, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(201) | |
end | |
it 'returns the data in the body' do | |
user = User.last | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:user][:email]).to eql(user.email) | |
expect(body[:user][:first_name]).to eql(user.first_name) | |
expect(body[:user][:last_name]).to eql(user.last_name) | |
end | |
end | |
context :show do | |
before do | |
@user = FactoryGirl.create(:user) | |
end | |
context 'with regular user' do | |
before do | |
sign_in(@user) | |
get api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the data in the body' do | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:user][:email]).to eql(@user.email) | |
expect(body[:user][:first_name]).to eql(@user.first_name) | |
expect(body[:user][:last_name]).to eql(@user.last_name) | |
end | |
end | |
context 'with another user' do | |
before do | |
create_and_sign_in_another_user | |
get api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns unauthorized status for wrong user' do | |
expect(last_response.status).to eql(401) | |
end | |
end | |
context 'with super_admin' do | |
before do | |
create_and_sign_in_super_admin | |
get api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns the user with 200 status' do | |
expect(last_response.status).to eql(200) | |
end | |
end | |
end | |
context :update do | |
before do | |
@user = FactoryGirl.create(:user) | |
sign_in(@user) | |
@email = '[email protected]' | |
@user.email = @email | |
end | |
context 'with regular user' do | |
before do | |
put api_v1_user_path(@user.id), user: @user.as_json, format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(200) | |
end | |
it 'returns the correct location' do | |
expect(last_response.headers['Location']) | |
.to include(api_v1_user_path(@user.id)) | |
end | |
it 'returns the data in the body' do | |
user = User.last | |
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body)) | |
expect(body[:user][:email]).to eql(@email) | |
expect(body[:user][:first_name]).to eql(user.first_name) | |
expect(body[:user][:last_name]).to eql(user.last_name) | |
end | |
end | |
context 'with another user' do | |
before do | |
create_and_sign_in_another_user | |
put api_v1_user_path(@user.id), user: @user.as_json, format: :json | |
end | |
it 'returns unauthorized status for wrong user' do | |
expect(last_response.status).to eql(401) | |
end | |
end | |
context 'with super_admin' do | |
before do | |
create_and_sign_in_super_admin | |
put api_v1_user_path(@user.id), user: @user.as_json, format: :json | |
end | |
it 'returns the user with 200 status' do | |
expect(last_response.status).to eql(200) | |
end | |
end | |
end | |
context :delete do | |
context 'when the resource does NOT exist' do | |
before do | |
@user = FactoryGirl.create(:user) | |
sign_in(@user) | |
delete api_v1_user_path(rand(100..1000)), format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(404) | |
end | |
end | |
context 'when the resource does exist' do | |
around(:each) do | |
@user = FactoryGirl.create(:user) | |
sign_in(@user) | |
end | |
context 'with regular user' do | |
before(:each) do | |
puts 'delete1' | |
delete api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns the correct status' do | |
expect(last_response.status).to eql(204) | |
end | |
it 'actually deletes the resource' do | |
expect(User.find_by(id: @user.id)).to eql(nil) | |
end | |
end | |
context 'with another user' do | |
before(:each) do | |
create_and_sign_in_another_user | |
delete api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns unauthorized status for wrong user' do | |
expect(last_response.status).to eql(401) | |
end | |
end | |
context 'with super_admin' do | |
before(:each) do | |
create_and_sign_in_super_admin | |
delete api_v1_user_path(@user.id), format: :json | |
end | |
it 'returns unauthorized status for wrong user' do | |
expect(last_response.status).to eql(204) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment