fix rspecs

This commit is contained in:
Tanmay Deep Sharma
2025-07-31 14:29:54 +05:30
parent b5f3daafc9
commit 24c7171a4e
9 changed files with 751 additions and 954 deletions
@@ -12,8 +12,8 @@ RSpec.describe 'Leaves API', type: :request do
describe 'GET /api/v1/accounts/:account_id/leaves' do
context 'when authenticated as an agent' do
it 'returns only their own leaves' do
create(:leave, account_user: agent_account_user)
create(:leave, account_user: account.account_users.find_by(user: another_agent))
leave1 = create(:leave, account_user: agent_account_user, account: account)
create(:leave, account_user: account.account_users.find_by(user: another_agent), account: account)
get "/api/v1/accounts/#{account.id}/leaves",
headers: agent.create_new_auth_token,
@@ -22,13 +22,14 @@ RSpec.describe 'Leaves API', type: :request do
expect(response).to have_http_status(:success)
json_response = response.parsed_body
expect(json_response['leaves'].size).to eq(1)
expect(json_response['leaves'].first['id']).to eq(leave1.id)
end
end
context 'when authenticated as an admin' do
it 'returns all leaves in the account' do
create(:leave, account_user: agent_account_user)
create(:leave, account_user: account.account_users.find_by(user: another_agent))
create(:leave, account_user: agent_account_user, account: account)
create(:leave, account_user: account.account_users.find_by(user: another_agent), account: account)
get "/api/v1/accounts/#{account.id}/leaves",
headers: admin.create_new_auth_token,
@@ -87,7 +88,7 @@ RSpec.describe 'Leaves API', type: :request do
end
describe 'PUT /api/v1/accounts/:account_id/leaves/:id' do
let(:leave) { create(:leave, account_user: agent_account_user) }
let(:leave) { create(:leave, account_user: agent_account_user, account: account) }
context 'when authenticated as the leave owner' do
it 'updates pending leave' do
@@ -122,13 +123,15 @@ RSpec.describe 'Leaves API', type: :request do
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:forbidden)
expect(response).to have_http_status(:unauthorized)
json_response = response.parsed_body
expect(json_response['error']).to eq('You are not authorized to do this action')
end
end
end
describe 'POST /api/v1/accounts/:account_id/leaves/:id/approve' do
let(:leave) { create(:leave, account_user: agent_account_user) }
let(:leave) { create(:leave, account_user: agent_account_user, account: account) }
context 'when authenticated as an admin' do
it 'approves the leave' do
@@ -145,18 +148,20 @@ RSpec.describe 'Leaves API', type: :request do
end
context 'when authenticated as a regular agent' do
it 'returns forbidden' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/leaves/#{leave.id}/approve",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:forbidden)
expect(response).to have_http_status(:unauthorized)
json_response = response.parsed_body
expect(json_response['error']).to eq('You are not authorized to do this action')
end
end
end
describe 'POST /api/v1/accounts/:account_id/leaves/:id/reject' do
let(:leave) { create(:leave, account_user: agent_account_user) }
let(:leave) { create(:leave, account_user: agent_account_user, account: account) }
context 'when authenticated as an admin' do
it 'rejects the leave with reason' do
@@ -185,7 +190,7 @@ RSpec.describe 'Leaves API', type: :request do
describe 'DELETE /api/v1/accounts/:account_id/leaves/:id' do
context 'when deleting own pending leave' do
let(:leave) { create(:leave, account_user: agent_account_user) }
let(:leave) { create(:leave, account_user: agent_account_user, account: account) }
it 'deletes the leave' do
delete "/api/v1/accounts/#{account.id}/leaves/#{leave.id}",
@@ -198,14 +203,16 @@ RSpec.describe 'Leaves API', type: :request do
end
context 'when trying to delete approved leave' do
let(:leave) { create(:leave, :approved, account_user: agent_account_user) }
let(:leave) { create(:leave, :approved, account_user: agent_account_user, account: account) }
it 'returns forbidden' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/leaves/#{leave.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:forbidden)
expect(response).to have_http_status(:unauthorized)
json_response = response.parsed_body
expect(json_response['error']).to eq('You are not authorized to do this action')
expect(Leave.find_by(id: leave.id)).to be_present
end
end