-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ff4c71
commit afdd4e8
Showing
4 changed files
with
90 additions
and
1 deletion.
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
71 changes: 71 additions & 0 deletions
71
spec/requests/internal_api/v1/invoices/send_invoice_spec.rb
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,71 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "InternalApi::V1::Invoices#send_invoice", type: :request do | ||
let(:client) { create :client_with_invoices } | ||
let(:company) { client.company } | ||
let(:invoice) { client.invoices.first } | ||
let(:user) { create :user, current_workspace_id: company.id } | ||
|
||
context "when user is signed in" do | ||
before do | ||
create(:company_user, company:, user:) | ||
end | ||
|
||
let(:invoice_email) do | ||
{ subject: "Some Subject", recipients: [client.email, "miru@example.com"], body: "You have an invoice!" } | ||
end | ||
|
||
context "when user is an admin" do | ||
before do | ||
user.add_role :admin, company | ||
sign_in user | ||
end | ||
|
||
it "returns a 202 response" do | ||
post send_invoice_internal_api_v1_invoice_path(id: invoice.id), params: { invoice_email: } | ||
|
||
expect(response).to have_http_status :accepted | ||
expect(json_response["message"]).to eq("Invoice will be sent!") | ||
end | ||
|
||
it "enqueues an email for delivery" do | ||
expect do | ||
post send_invoice_internal_api_v1_invoice_path(id: invoice.id), params: { invoice_email: } | ||
end.to have_enqueued_mail(InvoiceMailer, :invoice) | ||
end | ||
|
||
context "when invoice doesn't exist" do | ||
it "returns 404 response" do | ||
post send_invoice_internal_api_v1_invoice_path(id: "random") | ||
|
||
expect(response).to have_http_status :not_found | ||
expect(json_response["errors"]).to eq "Couldn't find Invoice with 'id'=random" | ||
end | ||
end | ||
end | ||
|
||
context "when user is an employee" do | ||
before do | ||
user.add_role :employee, company | ||
sign_in user | ||
end | ||
|
||
it "returns a 403 response" do | ||
post send_invoice_internal_api_v1_invoice_path(id: invoice.id) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
end | ||
|
||
context "when user is logged out" do | ||
it "returns a 401 response" do | ||
post send_invoice_internal_api_v1_invoice_path(id: invoice.id) | ||
|
||
expect(response).to have_http_status(:unauthorized) | ||
expect(json_response["error"]).to eq("You need to sign in or sign up before continuing.") | ||
end | ||
end | ||
end |