-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from dmitry-sinina/1.7.5
1.7.5
- Loading branch information
Showing
8 changed files
with
243 additions
and
6 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
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,2 @@ | ||
class Api::Rest::Admin::ContactsController < Api::Rest::Admin::BaseController | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Api::Rest::Admin::ContactResource < ::BaseResource | ||
model_name 'Billing::Contact' | ||
|
||
attributes :email, :notes | ||
|
||
has_one :contractor | ||
|
||
def self.records(options = {}) | ||
super(options).contractors | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'spec_helper' | ||
require 'rspec_api_documentation/dsl' | ||
|
||
resource 'Contacts' do | ||
header 'Accept', 'application/vnd.api+json' | ||
header 'Content-Type', 'application/vnd.api+json' | ||
header 'Authorization', :auth_token | ||
|
||
let(:user) { create :admin_user } | ||
let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } | ||
let(:type) { 'contacts' } | ||
|
||
required_params = %i(email) | ||
|
||
optional_params = %i(notes) | ||
|
||
required_relationships = %i() | ||
optional_relationships = %i(contractor) | ||
|
||
get '/api/rest/admin/contacts' do | ||
before { create_list(:contact, 2) } | ||
|
||
example_request 'get listing' do | ||
expect(status).to eq(200) | ||
end | ||
end | ||
|
||
get '/api/rest/admin/contacts/:id' do | ||
let(:id) { create(:contact).id } | ||
|
||
example_request 'get specific entry' do | ||
expect(status).to eq(200) | ||
end | ||
end | ||
|
||
post '/api/rest/admin/contacts' do | ||
parameter :type, 'Resource type (contacts)', scope: :data, required: true | ||
|
||
jsonapi_attributes(required_params, optional_params) | ||
jsonapi_relationships(required_relationships, optional_relationships) | ||
|
||
let(:new_customer) { create :customer } | ||
|
||
let(:email) { '[email protected]' } | ||
let(:notes) { 'Text here...' } | ||
let(:customer) { wrap_relationship(:contractors, new_customer.id) } | ||
|
||
example_request 'create new entry' do | ||
expect(status).to eq(201) | ||
end | ||
end | ||
|
||
put '/api/rest/admin/contacts/:id' do | ||
parameter :type, 'Resource type (contacts)', scope: :data, required: true | ||
parameter :id, 'Contact ID', scope: :data, required: true | ||
|
||
jsonapi_attributes(required_params, optional_params) | ||
|
||
let(:id) { create(:contact).id } | ||
let(:email) { '[email protected]' } | ||
|
||
example_request 'update values' do | ||
expect(status).to eq(200) | ||
end | ||
end | ||
|
||
delete '/api/rest/admin/contacts/:id' do | ||
let(:id) { create(:contact).id } | ||
|
||
example_request 'delete entry' do | ||
expect(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryGirl.define do | ||
factory :contact, class: Billing::Contact do | ||
sequence(:email) { |n| "rspec_mail_#{n}@example.com" } | ||
|
||
association :contractor, factory: :customer | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Api::Rest::Admin::ContactsController, type: :request do | ||
include_context :json_api_admin_helpers, type: :'contacts' | ||
|
||
describe 'GET /api/rest/admin/contacts' do | ||
subject do | ||
puts json_api_request_path | ||
get json_api_request_path, params: nil, headers: json_api_request_headers | ||
end | ||
|
||
let!(:contacts) do | ||
FactoryGirl.create_list(:contact, 2) # only Contact with contractor | ||
end | ||
|
||
include_examples :returns_json_api_collection do | ||
let(:json_api_collection_ids) do | ||
contacts.map { |r| r.id.to_s } | ||
end | ||
end | ||
end | ||
|
||
|
||
describe 'GET /api/rest/admin/contacts/{id}' do | ||
subject do | ||
get json_api_request_path, params: request_query, headers: json_api_request_headers | ||
end | ||
|
||
let(:json_api_request_path) { "#{super()}/#{record_id}" } | ||
let(:request_query) { nil } | ||
let(:record_id) { contact.id.to_s } | ||
|
||
let!(:contact) { FactoryGirl.create(:contact, contact_attrs) } | ||
let(:contractor) { contact.contractor } | ||
let(:contact_attrs) { { email: '[email protected]', notes: 'Text here...' } } | ||
let(:contact_response_attributes) do | ||
{ | ||
'email': contact.email, | ||
'notes': contact.notes | ||
} | ||
end | ||
|
||
include_examples :returns_json_api_record, relationships: [:contractor] do | ||
let(:json_api_record_id) { record_id } | ||
let(:json_api_record_attributes) { contact_response_attributes } | ||
end | ||
|
||
context 'with include destination' do | ||
let(:request_query) { { include: 'contractor' } } | ||
|
||
include_examples :returns_json_api_record_relationship, :contractor do | ||
let(:json_api_relationship_data) { { id: contractor.id.to_s, type: 'contractors' } } | ||
end | ||
|
||
include_examples :returns_json_api_record_include, type: :contractors do | ||
let(:json_api_include_id) { contractor.id.to_s } | ||
let(:json_api_include_attributes) { hash_including(name: contractor.name) } | ||
end | ||
end | ||
end | ||
|
||
|
||
describe 'POST /api/rest/admin/contacts' do | ||
subject do | ||
post json_api_request_path, params: json_api_request_body.to_json, headers: json_api_request_headers | ||
end | ||
|
||
let(:contractor) { FactoryGirl.create(:customer) } | ||
|
||
let(:json_api_request_body) do | ||
{ | ||
data: { | ||
type: json_api_resource_type, | ||
attributes: json_api_request_attributes, | ||
relationships: json_api_request_relationships | ||
} | ||
} | ||
end | ||
let(:json_api_request_attributes) do | ||
{ | ||
'email': '[email protected]', | ||
'notes': 'Text here...' | ||
} | ||
end | ||
let(:json_api_request_relationships) do | ||
{ | ||
contractor: { data: { id: contractor.id.to_s, type: 'contractors' } } | ||
} | ||
end | ||
let(:last_contact) { Billing::Contact.last! } | ||
|
||
include_examples :returns_json_api_record, relationships: [:contractor], status: 201 do | ||
let(:json_api_record_id) { last_contact.id.to_s } | ||
let(:json_api_record_attributes) do | ||
{ | ||
'email': json_api_request_attributes[:'email'], | ||
'notes': json_api_request_attributes[:'notes'] | ||
} | ||
end | ||
end | ||
|
||
include_examples :changes_records_qty_of, Billing::Contact, by: 1 | ||
end | ||
|
||
|
||
|
||
describe 'PATCH /api/rest/admin/contacts/{id}' do | ||
subject do | ||
patch json_api_request_path, params: json_api_request_body.to_json, headers: json_api_request_headers | ||
end | ||
|
||
let(:json_api_request_path) { "#{super()}/#{record_id}" } | ||
let(:record_id) { contact.id.to_s } | ||
let(:json_api_request_body) do | ||
{ data: { id: record_id, type: json_api_resource_type, attributes: json_api_request_attributes } } | ||
end | ||
let(:json_api_request_attributes) { { 'email': 'another@mail,com' } } | ||
|
||
let!(:contact) { FactoryGirl.create(:contact) } | ||
|
||
include_examples :returns_json_api_record, relationships: [:contractor] do | ||
let(:json_api_record_id) { contact.id.to_s } | ||
let(:json_api_record_attributes) do | ||
hash_including(json_api_request_attributes) | ||
end | ||
end | ||
end | ||
|
||
|
||
describe 'DELETE /api/rest/admin/contacts/{id}' do | ||
subject do | ||
delete json_api_request_path, headers: json_api_request_headers | ||
end | ||
|
||
let(:json_api_request_path) { "#{super()}/#{record_id}" } | ||
let(:request_query) { nil } | ||
let(:record_id) { contact.id.to_s } | ||
|
||
let!(:contact) { FactoryGirl.create(:contact) } | ||
|
||
include_examples :responds_with_status, 204 | ||
include_examples :changes_records_qty_of, Billing::Contact, by: -1 | ||
end | ||
end |