-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
8 changed files
with
217 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module API | ||
module V1 | ||
class AddressesController < BaseController | ||
def index | ||
respond_with_resource(beneficiary.addresses) | ||
end | ||
|
||
def create | ||
validate_request_schema( | ||
with: ::V1::BeneficiaryAddressRequestSchema, | ||
location: ->(resource) { api_v1_beneficiary_address_path(beneficiary, resource) } | ||
) do |permitted_params| | ||
beneficiary.addresses.create!(permitted_params) | ||
end | ||
end | ||
|
||
def show | ||
address = beneficiary.addresses.find(params[:id]) | ||
respond_with_resource(address) | ||
end | ||
|
||
def destroy | ||
address = beneficiary.addresses.find(params[:id]) | ||
address.destroy! | ||
|
||
head :no_content | ||
end | ||
|
||
def beneficiary | ||
@beneficiary ||= current_account.beneficiaries.find(params[:beneficiary_id]) | ||
end | ||
end | ||
end | ||
end |
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
22 changes: 22 additions & 0 deletions
22
app/request_schemas/v1/beneficiary_address_request_schema.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,22 @@ | ||
module V1 | ||
class BeneficiaryAddressRequestSchema < BaseRequestSchema | ||
params do | ||
required(:data).value(:hash).schema do | ||
required(:type).filled(:str?, eql?: "address") | ||
required(:attributes).value(:hash).schema do | ||
required(:iso_region_code).maybe(:string) | ||
optional(:administrative_division_level_2_code).maybe(:string) | ||
optional(:administrative_division_level_2_name).maybe(:string) | ||
optional(:administrative_division_level_3_code).maybe(:string) | ||
optional(:administrative_division_level_3_name).maybe(:string) | ||
optional(:administrative_division_level_4_code).maybe(:string) | ||
optional(:administrative_division_level_4_name).maybe(:string) | ||
end | ||
end | ||
end | ||
|
||
def output | ||
super.except(:account) | ||
end | ||
end | ||
end |
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
84 changes: 84 additions & 0 deletions
84
spec/requests/open_ews_api/v1/beneficiaries/addresses_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,84 @@ | ||
require "rails_helper" | ||
|
||
RSpec.resource "Beneficiary's Addresses" do | ||
get "/v1/beneficiaries/:beneficiary_id/addresses" do | ||
example "List all a beneficiary's addresses" do | ||
account = create(:account) | ||
beneficiary = create(:beneficiary, account:) | ||
address1 = create(:address, beneficiary:) | ||
address2 = create(:address, beneficiary:) | ||
other_beneficiary = create(:beneficiary) | ||
_other_address = create(:address, beneficiary: other_beneficiary) | ||
|
||
set_authorization_header_for(account) | ||
do_request(beneficiary_id: beneficiary.id) | ||
|
||
expect(response_status).to eq(200) | ||
expect(response_body).to match_jsonapi_resource_collection_schema("address") | ||
expect(json_response.fetch("data").pluck("id")).to contain_exactly( | ||
address1.id.to_s, | ||
address2.id.to_s | ||
) | ||
end | ||
end | ||
|
||
post "/v1/beneficiaries/:beneficiary_id/addresses" do | ||
example "Create an address for a beneficiary" do | ||
account = create(:account) | ||
beneficiary = create(:beneficiary, account:) | ||
|
||
set_authorization_header_for(account) | ||
do_request( | ||
beneficiary_id: beneficiary.id, | ||
data: { | ||
type: :address, | ||
attributes: { | ||
iso_region_code: "KH-1", | ||
administrative_division_level_2_code: "01" | ||
} | ||
} | ||
) | ||
|
||
expect(response_status).to eq(201) | ||
expect(response_body).to match_jsonapi_resource_schema("address") | ||
expect(jsonapi_response_attributes).to include( | ||
"iso_region_code" => "KH-1", | ||
"administrative_division_level_2_code" => "01" | ||
) | ||
end | ||
end | ||
|
||
get "/v1/beneficiaries/:beneficiary_id/addresses/:id" do | ||
example "Get an address for a beneficiary" do | ||
account = create(:account) | ||
beneficiary = create(:beneficiary, account:) | ||
address = create(:address, beneficiary:) | ||
|
||
set_authorization_header_for(account) | ||
do_request( | ||
beneficiary_id: beneficiary.id, | ||
id: address.id | ||
) | ||
|
||
expect(response_status).to eq(200) | ||
expect(response_body).to match_jsonapi_resource_schema("address") | ||
expect(json_response.dig("data", "id")).to eq(address.id.to_s) | ||
end | ||
end | ||
|
||
delete "/v1/beneficiaries/:beneficiary_id/addresses/:id" do | ||
example "Delete an address for a beneficiary" do | ||
account = create(:account) | ||
beneficiary = create(:beneficiary, account:) | ||
address = create(:address, beneficiary:) | ||
|
||
set_authorization_header_for(account) | ||
do_request( | ||
beneficiary_id: beneficiary.id, | ||
id: address.id | ||
) | ||
|
||
expect(response_status).to eq(204) | ||
end | ||
end | ||
end |
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