|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +RSpec.describe Api::V1::ProductsController do |
| 6 | + let(:organization) { create(:organization) } |
| 7 | + |
| 8 | + describe "POST /api/v1/products" do |
| 9 | + subject { post_with_token(organization, "/api/v1/products", {product: create_params}) } |
| 10 | + |
| 11 | + let(:create_params) do |
| 12 | + { |
| 13 | + name: "Cards", |
| 14 | + code: "cards", |
| 15 | + description: "Card products", |
| 16 | + invoice_display_name: "Cards" |
| 17 | + } |
| 18 | + end |
| 19 | + |
| 20 | + include_examples "requires API permission", "product", "write" |
| 21 | + |
| 22 | + it "creates a product" do |
| 23 | + subject |
| 24 | + |
| 25 | + expect(response).to have_http_status(:success) |
| 26 | + expect(json[:product][:lago_id]).to be_present |
| 27 | + expect(json[:product][:name]).to eq("Cards") |
| 28 | + expect(json[:product][:code]).to eq("cards") |
| 29 | + expect(json[:product][:description]).to eq("Card products") |
| 30 | + expect(json[:product][:invoice_display_name]).to eq("Cards") |
| 31 | + end |
| 32 | + |
| 33 | + context "when the code is already used" do |
| 34 | + before { create(:product, organization:, code: "cards") } |
| 35 | + |
| 36 | + it "returns a validation error" do |
| 37 | + subject |
| 38 | + |
| 39 | + expect(response).to have_http_status(:unprocessable_entity) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe "PUT /api/v1/products/:code" do |
| 45 | + subject { put_with_token(organization, "/api/v1/products/#{product.code}", {product: update_params}) } |
| 46 | + |
| 47 | + let(:product) { create(:product, organization:, name: "Before") } |
| 48 | + let(:update_params) { {name: "After"} } |
| 49 | + |
| 50 | + include_examples "requires API permission", "product", "write" |
| 51 | + |
| 52 | + it "updates the product" do |
| 53 | + subject |
| 54 | + |
| 55 | + expect(response).to have_http_status(:success) |
| 56 | + expect(json[:product][:name]).to eq("After") |
| 57 | + expect(json[:product][:code]).to eq(product.code) |
| 58 | + end |
| 59 | + |
| 60 | + context "when the product does not exist" do |
| 61 | + subject { put_with_token(organization, "/api/v1/products/unknown", {product: update_params}) } |
| 62 | + |
| 63 | + it "returns a not found error" do |
| 64 | + subject |
| 65 | + |
| 66 | + expect(response).to be_not_found_error("product") |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe "GET /api/v1/products/:code" do |
| 72 | + subject { get_with_token(organization, "/api/v1/products/#{product.code}") } |
| 73 | + |
| 74 | + let(:product) { create(:product, organization:) } |
| 75 | + |
| 76 | + include_examples "requires API permission", "product", "read" |
| 77 | + |
| 78 | + it "returns the product" do |
| 79 | + subject |
| 80 | + |
| 81 | + expect(response).to have_http_status(:success) |
| 82 | + expect(json[:product][:lago_id]).to eq(product.id) |
| 83 | + expect(json[:product][:code]).to eq(product.code) |
| 84 | + end |
| 85 | + |
| 86 | + context "when the product does not exist" do |
| 87 | + subject { get_with_token(organization, "/api/v1/products/unknown") } |
| 88 | + |
| 89 | + it "returns a not found error" do |
| 90 | + subject |
| 91 | + |
| 92 | + expect(response).to be_not_found_error("product") |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context "when the product belongs to another organization" do |
| 97 | + let(:product) { create(:product) } |
| 98 | + |
| 99 | + it "returns a not found error" do |
| 100 | + subject |
| 101 | + |
| 102 | + expect(response).to be_not_found_error("product") |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "GET /api/v1/products" do |
| 108 | + subject { get_with_token(organization, "/api/v1/products?page=1&per_page=1") } |
| 109 | + |
| 110 | + before { create(:product, organization:) } |
| 111 | + |
| 112 | + include_examples "requires API permission", "product", "read" |
| 113 | + |
| 114 | + it "returns the paginated products" do |
| 115 | + create(:product, organization:) |
| 116 | + |
| 117 | + subject |
| 118 | + |
| 119 | + expect(response).to have_http_status(:success) |
| 120 | + expect(json[:products].count).to eq(1) |
| 121 | + expect(json[:meta][:total_count]).to eq(2) |
| 122 | + expect(json[:products].first[:lago_id]).to be_present |
| 123 | + end |
| 124 | + |
| 125 | + it "does not return products from other organizations" do |
| 126 | + other = create(:product) |
| 127 | + |
| 128 | + subject |
| 129 | + |
| 130 | + expect(json[:products].map { it[:lago_id] }).not_to include(other.id) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + describe "DELETE /api/v1/products/:code" do |
| 135 | + subject { delete_with_token(organization, "/api/v1/products/#{product.code}") } |
| 136 | + |
| 137 | + let(:product) { create(:product, organization:) } |
| 138 | + |
| 139 | + include_examples "requires API permission", "product", "write" |
| 140 | + |
| 141 | + it "soft deletes the product" do |
| 142 | + subject |
| 143 | + |
| 144 | + expect(response).to have_http_status(:success) |
| 145 | + expect(json[:product][:lago_id]).to eq(product.id) |
| 146 | + expect(product.reload).to be_discarded |
| 147 | + end |
| 148 | + |
| 149 | + context "when the product does not exist" do |
| 150 | + subject { delete_with_token(organization, "/api/v1/products/unknown") } |
| 151 | + |
| 152 | + it "returns a not found error" do |
| 153 | + subject |
| 154 | + |
| 155 | + expect(response).to be_not_found_error("product") |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments