Skip to content

Commit

Permalink
Merge pull request #361 from dmitry-sinina/1.7.5
Browse files Browse the repository at this point in the history
1.7.5
  • Loading branch information
dmitry-sinina authored Sep 4, 2018
2 parents 64cd168 + dea1159 commit ef40204
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 6 deletions.
8 changes: 2 additions & 6 deletions app/admin/cdr/cdrs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def scoped_collection
end


index download_links: false do
index do
column :id do |cdr|
if cdr.dump_level_id>0
link_to( cdr.id, resource_path(cdr), class: "resource_id_link", title: 'Details') + " " + link_to(fa_icon('exchange'), dump_cdr_path(cdr), title: 'Download trace')
Expand Down Expand Up @@ -847,7 +847,7 @@ def scoped_collection
column :early_media_present


when 'all'
else #all or not defined policy
column :id
column :time_start
column :time_connect
Expand Down Expand Up @@ -986,10 +986,6 @@ def scoped_collection
column :legb_rx_bytes
column :legb_tx_bytes


else
logger.error "unknown csv_policy '#{policy}'"
raise "unknown csv_policy '#{policy}'"
end


Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/rest/admin/contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Api::Rest::Admin::ContactsController < Api::Rest::Admin::BaseController
end
2 changes: 2 additions & 0 deletions app/models/billing/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Billing::Contact < Yeti::ActiveRecord
belongs_to :contractor, class_name: 'Contractor', foreign_key: :contractor_id
belongs_to :admin_user, class_name: 'AdminUser', foreign_key: :admin_user_id

scope :contractors, -> { where.not(contractor_id: nil) }

before_destroy do
Report::CustomerTrafficScheduler.where("? = ANY(send_to)", self.id).each do |c|
c.send_to = c.send_to.reject { |el| el == self.id }
Expand Down
11 changes: 11 additions & 0 deletions app/resources/api/rest/admin/contact_resource.rb
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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
jsonapi_resource :balance, only: [:update]
end
jsonapi_resources :contractors
jsonapi_resources :contacts
jsonapi_resources :api_accesses
jsonapi_resources :customers_auths
jsonapi_resources :destinations
Expand Down
74 changes: 74 additions & 0 deletions spec/acceptance/rest/admin/api/contacts_spec.rb
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
7 changes: 7 additions & 0 deletions spec/factories/billing/contact.rb
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
144 changes: 144 additions & 0 deletions spec/requests/api/rest/admin/contacts_spec.rb
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

0 comments on commit ef40204

Please sign in to comment.