-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added endpoint for get cancelled invoices
- Loading branch information
1 parent
e6506c8
commit 225a50e
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
app/controllers/api/v1/accreditation_center/invoice_status_controller.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,32 @@ | ||
module Api | ||
module V1 | ||
module AccreditationCenter | ||
class InvoiceStatusController < ::Api::V1::AccreditationCenter::BaseController | ||
def index | ||
username, password = Base64.urlsafe_decode64(basic_token).split(':') | ||
@current_user ||= ApiUser.find_by(username: username, plain_text_password: password) | ||
|
||
return render json: { errors: 'No user found' }, status: :not_found if @current_user.nil? | ||
|
||
@invoices = @current_user.registrar.invoices.select { |i| i.cancelled_at != nil } | ||
|
||
if @invoices | ||
render json: { code: 1000, invoices: @invoices }, | ||
status: :found | ||
else | ||
render json: { errors: 'No invoices' }, status: :not_found | ||
end | ||
end | ||
|
||
private | ||
|
||
def basic_token | ||
pattern = /^Basic / | ||
header = request.headers['Authorization'] | ||
header = header.gsub(pattern, '') if header&.match(pattern) | ||
header.strip | ||
end | ||
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
33 changes: 33 additions & 0 deletions
33
test/integration/api/accreditation_center/invoice_status_test.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,33 @@ | ||
require 'test_helper' | ||
|
||
class DomainsTest < ApplicationIntegrationTest | ||
def setup | ||
super | ||
|
||
@user = users(:api_bestnames) | ||
@header = { 'Authorization' => "Basic #{generate_base64}" } | ||
end | ||
|
||
def test_should_return_cancelled_invoices | ||
date_now = Time.now | ||
|
||
get "/api/v1/accreditation_center/invoice_status", headers: @header | ||
json = JSON.parse(response.body, symbolize_names: true) | ||
|
||
assert_equal json[:invoices].count, 0 | ||
|
||
invoice = @user.registrar.invoices.last | ||
invoice.update(cancelled_at: date_now) | ||
|
||
get "/api/v1/accreditation_center/invoice_status", headers: @header | ||
json = JSON.parse(response.body, symbolize_names: true) | ||
|
||
assert_equal json[:invoices].count, 1 | ||
end | ||
|
||
private | ||
|
||
def generate_base64 | ||
Base64.encode64("#{@user.username}:#{@user.plain_text_password}") | ||
end | ||
end |