Repository URL to install this package:
|
Version:
2.23.0 ▾
|
require 'spec_helper'
describe JPlatform::UsersController, :type => :controller do
include_context "controller setup"
before(:each) do
Rails.cache.clear
stub_get("/api/v1/projects/1.json?include_social_accounts=true").to_return(body: fixture("project_with_members_and_tickets.json"), headers: {content_type: "application/json; charset=utf-8"})
end
header = nil
describe 'Organization has no project' do
before(:each) do
header = header
stub_get("/api/v1/organizations/1.json").to_return(body: fixture("organizations.json"), headers: header)
session[:user_id] = 2
end
it 'allows member to access' do
stub_get("/api/v1/users/me.json").to_return(body: fixture("member_without_project.json"), headers: header)
get :show, id: 'me', use_route: :user_path
expect(response).to render_template :show
end
it 'allows super admin to access' do
stub_get("/api/v1/users/me.json").to_return(body: fixture("admin_without_project.json"), headers: header)
get :show, id: 'me', use_route: :user_path
expect(response).to render_template :show
end
end
describe "Organization has projects" do
before(:each) do
stub_get("/api/v1/organizations/1.json").to_return(body: fixture("organizations.json"), headers: header)
end
describe "GET 'show'" do
context "shows user's own profile" do
it "renders user profile page of the current user" do
get :show, id: 'me', use_route: :user_path
expect(controller.current_user.id).to eq(session[:user_id])
expect(response).to render_template :show
end
it "retrieves current user profile" do
get :show, id: 'me', use_route: :user_path
expect(a_get("/api/v1/users/me.json")).to have_been_made
expect(assigns(:user)).to eq(controller.current_user)
end
end
context "shows specific user's profile" do
before(:each) do
stub_get("/api/v1/users/2.json").to_return(body: fixture("member_without_project.json"), headers: header)
end
it "renders user profile page of the requested user and retrieves the user profile" do
get :show, id: 2, use_route: :user_path
expect(response).to render_template :show
expect(a_get("/api/v1/users/2.json")).to have_been_made
expect(assigns(:user).id).to eq(2)
end
end
end
describe "PATCH 'update'" do
context "update user details" do
before(:each) do
stub_put("/api/v1/users/1.json").with(body: "{\"user\":{\"id\":1,\"first_name\":\"abc\",\"last_name\":\"def\",\"email\":\"test@test.com\",\"full_name\":\"Jugnoo Dev\",\"role\":\"super_admin\",\"status\":\"active\",\"created_at\":\"2013-04-23T13:45:31.803Z\",\"avatar\":{\"url_thumb\":\"http://j-platform.dev/assets/avatars/thumb/missing.png\",\"url_medium\":\"http://j-platform.dev/assets/avatars/medium/missing.png\",\"url\":\"http://j-platform.dev/assets/avatars/original/missing.png\"},\"organization\":{\"id\":1,\"name\":\"Jugnoo\",\"super_admin\":{\"id\":1,\"first_name\":\"Jugnoo\",\"last_name\":\"Dev\",\"email\":\"dev@jugnoo.com\",\"role\":\"super_admin\",\"preferred_timezone\":\"UTC\",\"organization_id\":1,\"crypted_password\":\"$2a$10$uDSn0WAnGHPaZk2mYfvZN.UNFz00uHWIYG/leEmErbdEzkwBb0Yfe\",\"salt\":\"FthUheCdv9TLtx5q7Hyy\",\"is_destroyed\":false,\"is_suspended\":false,\"is_pending\":false,\"created_at\":\"2013-04-23T13:45:31.803Z\",\"updated_at\":\"2013-04-23T13:45:33.108Z\",\"activation_token\":null,\"activation_token_expires_at\":null,\"remember_me_token\":null,\"remember_me_token_expires_at\":null,\"reset_password_token\":null,\"reset_password_token_expires_at\":null,\"reset_password_email_sent_at\":null,\"avatar_file_name\":null,\"avatar_content_type\":null,\"avatar_file_size\":null,\"avatar_updated_at\":null},\"created_at\":\"2013-04-23T13:45:31.327Z\"},\"clients\":[{\"id\":1,\"name\":\"Coca Cola\",\"description\":null,\"status\":\"active\",\"is_suspended\":false,\"created_at\":\"2013-04-23T13:45:33.734Z\",\"organization_id\":1,\"admin_ids\":[1],\"admins\":[{\"id\":1,\"first_name\":\"Jugnoo\",\"last_name\":\"Dev\",\"email\":\"dev@jugnoo.com\",\"full_name\":\"Jugnoo Dev\",\"role\":\"super_admin\",\"status\":\"active\",\"created_at\":\"2013-04-23T13:45:31.803Z\",\"avatar\":{\"url_thumb\":\"http://j-platform.dev/assets/avatars/thumb/missing.png\",\"url_medium\":\"http://j-platform.dev/assets/avatars/medium/missing.png\",\"url\":\"http://j-platform.dev/assets/avatars/original/missing.png\"},\"organization\":{\"id\":1,\"name\":\"Jugnoo\",\"super_admin\":{\"id\":1,\"first_name\":\"Jugnoo\",\"last_name\":\"Dev\",\"email\":\"dev@jugnoo.com\",\"role\":\"super_admin\",\"preferred_timezone\":\"UTC\",\"organization_id\":1,\"crypted_password\":\"$2a$10$uDSn0WAnGHPaZk2mYfvZN.UNFz00uHWIYG/leEmErbdEzkwBb0Yfe\",\"salt\":\"FthUheCdv9TLtx5q7Hyy\",\"is_destroyed\":false,\"is_suspended\":false,\"is_pending\":false,\"created_at\":\"2013-04-23T13:45:31.803Z\",\"updated_at\":\"2013-04-23T13:45:33.108Z\",\"activation_token\":null,\"activation_token_expires_at\":null,\"remember_me_token\":null,\"remember_me_token_expires_at\":null,\"reset_password_token\":null,\"reset_password_token_expires_at\":null,\"reset_password_email_sent_at\":null,\"avatar_file_name\":null,\"avatar_content_type\":null,\"avatar_file_size\":null,\"avatar_updated_at\":null},\"created_at\":\"2013-04-23T13:45:31.327Z\"}}],\"projects_count\":1,\"social_accounts_count\":3,\"members_count\":5,\"ticket_lists_count\":2,\"avatar\":{\"url_thumb\":\"http://j-platform.dev/assets/avatars/thumb/missing.png\",\"url_medium\":\"http://j-platform.dev/assets/avatars/medium/missing.png\",\"url_large\":\"http://j-platform.dev/assets/avatars/large/missing.png\",\"url\":\"http://j-platform.dev/assets/avatars/original/missing.png\"},\"organization\":{\"id\":1,\"name\":\"Jugnoo\",\"super_admin\":{\"id\":1,\"first_name\":\"Jugnoo\",\"last_name\":\"Dev\",\"email\":\"dev@jugnoo.com\",\"role\":\"super_admin\",\"preferred_timezone\":\"UTC\",\"organization_id\":1,\"crypted_password\":\"$2a$10$uDSn0WAnGHPaZk2mYfvZN.UNFz00uHWIYG/leEmErbdEzkwBb0Yfe\",\"salt\":\"FthUheCdv9TLtx5q7Hyy\",\"is_destroyed\":false,\"is_suspended\":false,\"is_pending\":false,\"created_at\":\"2013-04-23T13:45:31.803Z\",\"updated_at\":\"2013-04-23T13:45:33.108Z\",\"activation_token\":null,\"activation_token_expires_at\":null,\"remember_me_token\":null,\"remember_me_token_expires_at\":null,\"reset_password_token\":null,\"reset_password_token_expires_at\":null,\"reset_password_email_sent_at\":null,\"avatar_file_name\":null,\"avatar_content_type\":null,\"avatar_file_size\":null,\"avatar_updated_at\":null},\"created_at\":\"2013-04-23T13:45:31.327Z\"},\"project_ids\":[1],\"social_account_ids\":[1,2,4],\"member_ids\":[1,5,4,3,2],\"ticket_list_ids\":[2,1]}],\"projects\":[{\"id\":1,\"name\":\"CocaCola Project 1\",\"description\":\"To kick Pepsi's ass\",\"status\":\"active\",\"created_at\":\"2013-04-23T13:45:33.925Z\",\"updated_at\":\"2013-04-23T13:45:33.925Z\",\"client_id\":1,\"social_accounts_count\":1,\"client_memberships_count\":3,\"ticket_lists_count\":2,\"avatar\":{\"url_thumb\":\"http://j-platform.dev/assets/avatars/thumb/missing.png\",\"url_medium\":\"http://j-platform.dev/assets/avatars/medium/missing.png\",\"url\":\"http://j-platform.dev/assets/avatars/original/missing.png\"},\"social_account_ids\":[4],\"client_membership_ids\":[1,4,6],\"project_ticket_list_ids\":[1,2],\"ticket_list_ids\":[1,2]}]}}",
:headers => {'Accept'=>'*/*', 'Authorization'=>'Bearer', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).to_return(status: 200, body: "", headers: {})
@user_attr = {email: 'test@test.com', first_name: 'abc', last_name: 'def'}
end
context "with proper email, first name, and last name" do
it "updates user details" do
put :update, use_route: :user_path, user: @user_attr
expect(assigns(:user).email).to eq(@user_attr[:email])
expect(assigns(:user).first_name).to eq(@user_attr[:first_name])
expect(assigns(:user).last_name).to eq(@user_attr[:last_name])
end
it "renders json success" do
put :update, use_route: :user_path, user: @user_attr
expect(JSON.parse(response.body)['status']).to be_truthy
end
end
context "with improper first name" do
before(:each) do
@user_attr[:first_name] = ""
end
it "does not update user details" do
put :update, use_route: :user_path, user: @user_attr
user = JPlatform::User.me
expect(user.first_name).not_to eq(@user_attr[:first_name])
end
it "renders json error" do
put :update, use_route: :user_path, user: @user_attr
expect(JSON.parse(response.body)['status']).to eq("error")
end
end
end
end
describe "DELETE #delete" do
it 'deletes a user' do
stub_get("/api/v1/users/2.json").to_return(body: fixture("member_without_project.json"), headers: header)
stub_delete("/api/v1/users/2.json").to_return(body: fixture("member_without_project.json"), headers: header)
delete :destroy, id: 2, use_route: :user_path
expect(a_delete("/api/v1/users/2.json")).to have_been_made
end
it "does not allow user to destroy itself" do
stub_get("/api/v1/users/1.json").to_return(body: fixture("user1.json"), headers: header)
stub_delete("/api/v1/users/1.json").to_return(body: fixture("user1.json"), headers: header)
delete :destroy, id: 1, use_route: :user_path
expect(a_delete("/api/v1/users/1.json")).not_to have_been_made
end
it 'returns status 404 when user is not found' do
stub_get("/api/v1/users/999.json").to_raise(ActiveResource::ResourceNotFound.new(''))
stub_delete("/api/v1/users/999.json").to_return(body: '', headers: header)
delete :destroy, id: 999, use_route: :user_path
expect(a_delete("/api/v1/users/999.json")).not_to have_been_made
expect(response.status).to eq(404)
end
end
end
end