diff --git a/.gitignore b/.gitignore index 69be5af..e08bff5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ Gemfile.lock example.rb /invoiced-*.gem -/coverage \ No newline at end of file +/coverage +.DS_Store diff --git a/lib/invoiced.rb b/lib/invoiced.rb index 92b8743..e846967 100644 --- a/lib/invoiced.rb +++ b/lib/invoiced.rb @@ -21,6 +21,7 @@ require 'invoiced/attachment' require 'invoiced/catalog_item' require 'invoiced/contact' +require 'invoiced/coupon' require 'invoiced/credit_note' require 'invoiced/customer' require 'invoiced/email' @@ -28,10 +29,15 @@ require 'invoiced/event' require 'invoiced/file' require 'invoiced/invoice' +require 'invoiced/letter' require 'invoiced/line_item' +require 'invoiced/note' require 'invoiced/payment_plan' require 'invoiced/plan' require 'invoiced/subscription' +require 'invoiced/task' +require 'invoiced/tax_rate' +require 'invoiced/text_message' require 'invoiced/transaction' module Invoiced @@ -43,7 +49,7 @@ class Client ReadTimeout = 80 attr_reader :api_key, :api_url, :sandbox, :sso_key - attr_reader :CatalogItem, :CreditNote, :Customer, :Estimate, :Event, :File, :Invoice, :Plan, :Subscription, :Transaction + attr_reader :CatalogItem, :Coupon, :CreditNote, :Customer, :Estimate, :Event, :File, :Invoice, :Note, :Plan, :Subscription, :Task, :TaxRate, :Transaction def initialize(api_key, sandbox=false, sso_key=false) @api_key = api_key @@ -53,14 +59,18 @@ def initialize(api_key, sandbox=false, sso_key=false) # Object endpoints @CatalogItem = Invoiced::CatalogItem.new(self) + @Coupon = Invoiced::Coupon.new(self) @CreditNote = Invoiced::CreditNote.new(self) @Customer = Invoiced::Customer.new(self) @Estimate = Invoiced::Estimate.new(self) @Event = Invoiced::Event.new(self) @File = Invoiced::File.new(self) @Invoice = Invoiced::Invoice.new(self) + @Note = Invoiced::Note.new(self) @Plan = Invoiced::Plan.new(self) @Subscription = Invoiced::Subscription.new(self) + @Task = Invoiced::Task.new(self) + @TaxRate = Invoiced::TaxRate.new(self) @Transaction = Invoiced::Transaction.new(self) end diff --git a/lib/invoiced/coupon.rb b/lib/invoiced/coupon.rb new file mode 100644 index 0000000..2130243 --- /dev/null +++ b/lib/invoiced/coupon.rb @@ -0,0 +1,10 @@ +module Invoiced + class Coupon < Object + include Invoiced::Operations::List + include Invoiced::Operations::Create + include Invoiced::Operations::Update + include Invoiced::Operations::Delete + + OBJECT_NAME = 'coupon' + end +end \ No newline at end of file diff --git a/lib/invoiced/credit_note.rb b/lib/invoiced/credit_note.rb index 338dea9..b0bb98e 100644 --- a/lib/invoiced/credit_note.rb +++ b/lib/invoiced/credit_note.rb @@ -35,5 +35,13 @@ def attachments(params={}) return attachments, metadata end + + def void() + response = @client.request(:post, "#{self.endpoint()}/void", {}) + + refresh_from(response[:body].dup.merge({:id => self.id})) + + return response[:code] == 200 + end end end \ No newline at end of file diff --git a/lib/invoiced/customer.rb b/lib/invoiced/customer.rb index 19a4435..8215ecc 100644 --- a/lib/invoiced/customer.rb +++ b/lib/invoiced/customer.rb @@ -15,6 +15,22 @@ def send_statement(params={}, opts={}) Util.build_objects(email, response[:body]) end + def send_statement_sms(params={}, opts={}) + response = @client.request(:post, "#{self.endpoint()}/text_messages", params, opts) + + # build text message objects + text_message = TextMessage.new(@client) + Util.build_objects(text_message, response[:body]) + end + + def send_statement_letter(params={}, opts={}) + response = @client.request(:post, "#{self.endpoint()}/letters", params, opts) + + # build letter objects + letter = Letter.new(@client) + Util.build_objects(letter, response[:body]) + end + def balance response = @client.request(:get, "#{self.endpoint()}/balance") @@ -31,6 +47,11 @@ def line_items() line.set_endpoint_base(self.endpoint()) end + def list_notes() + note = Note.new(@client) + note.set_endpoint_base(self.endpoint()) + end + def invoice(params={}, opts={}) response = @client.request(:post, "#{self.endpoint()}/invoices", params, opts) @@ -38,5 +59,13 @@ def invoice(params={}, opts={}) invoice = Invoice.new(@client) Util.convert_to_object(invoice, response[:body]) end + + def consolidate_invoices(params={}) + response = @client.request(:post, "#{self.endpoint()}/consolidate_invoices", params) + + # build invoice object + invoice = Invoice.new(@client) + Util.convert_to_object(invoice, response[:body]) + end end end \ No newline at end of file diff --git a/lib/invoiced/estimate.rb b/lib/invoiced/estimate.rb index 19c6544..b820461 100644 --- a/lib/invoiced/estimate.rb +++ b/lib/invoiced/estimate.rb @@ -43,5 +43,13 @@ def attachments(params={}) return attachments, metadata end + + def void() + response = @client.request(:post, "#{self.endpoint()}/void", {}) + + refresh_from(response[:body].dup.merge({:id => self.id})) + + return response[:code] == 200 + end end end \ No newline at end of file diff --git a/lib/invoiced/invoice.rb b/lib/invoiced/invoice.rb index fd8bbbe..f8003ad 100644 --- a/lib/invoiced/invoice.rb +++ b/lib/invoiced/invoice.rb @@ -15,6 +15,23 @@ def send(params={}, opts={}) Util.build_objects(email, response[:body]) end + def send_sms(params={}, opts={}) + response = @client.request(:post, "#{self.endpoint()}/text_messages", params, opts) + + # build text message objects + text_message = TextMessage.new(@client) + Util.build_objects(text_message, response[:body]) + end + + def send_letter(params={}, opts={}) + response = @client.request(:post, "#{self.endpoint()}/letters", params, opts) + + # build letter objects + + letter = Letter.new(@client) + Util.build_objects(letter, response[:body]) + end + def pay(opts={}) response = @client.request(:post, "#{self.endpoint()}/pay", {}, opts) @@ -33,7 +50,7 @@ def attachments(params={}) if !attachment.has_key?(:id) attachment[:id] = attachment[:file][:id] end - end + end # build objects attachment = Attachment.new(@client) @@ -49,5 +66,18 @@ def payment_plan() paymentPlan = PaymentPlan.new(@client) paymentPlan.set_endpoint_base(self.endpoint()) end + + def notes() + note = Note.new(@client) + note.set_endpoint_base(self.endpoint()) + end + + def void() + response = @client.request(:post, "#{self.endpoint()}/void", {}) + + refresh_from(response[:body].dup.merge({:id => self.id})) + + return response[:code] == 200 + end end end \ No newline at end of file diff --git a/lib/invoiced/letter.rb b/lib/invoiced/letter.rb new file mode 100644 index 0000000..66339ba --- /dev/null +++ b/lib/invoiced/letter.rb @@ -0,0 +1,5 @@ +module Invoiced + class Letter < Object + OBJECT_NAME = 'letter' + end +end \ No newline at end of file diff --git a/lib/invoiced/note.rb b/lib/invoiced/note.rb new file mode 100644 index 0000000..a8ac06e --- /dev/null +++ b/lib/invoiced/note.rb @@ -0,0 +1,10 @@ +module Invoiced + class Note < Object + include Invoiced::Operations::List + include Invoiced::Operations::Create + include Invoiced::Operations::Update + include Invoiced::Operations::Delete + + OBJECT_NAME = 'note' + end +end \ No newline at end of file diff --git a/lib/invoiced/subscription.rb b/lib/invoiced/subscription.rb index 3267318..73b01d8 100644 --- a/lib/invoiced/subscription.rb +++ b/lib/invoiced/subscription.rb @@ -10,5 +10,11 @@ class Subscription < Object def cancel delete end + + def preview(params={}, opts={}) + response = @client.request(:post, "/subscriptions/preview", params, opts) + + response[:body] + end end end \ No newline at end of file diff --git a/lib/invoiced/task.rb b/lib/invoiced/task.rb new file mode 100644 index 0000000..909f014 --- /dev/null +++ b/lib/invoiced/task.rb @@ -0,0 +1,10 @@ +module Invoiced + class Task < Object + include Invoiced::Operations::List + include Invoiced::Operations::Create + include Invoiced::Operations::Update + include Invoiced::Operations::Delete + + OBJECT_NAME = 'task' + end +end \ No newline at end of file diff --git a/lib/invoiced/tax_rate.rb b/lib/invoiced/tax_rate.rb new file mode 100644 index 0000000..ee77879 --- /dev/null +++ b/lib/invoiced/tax_rate.rb @@ -0,0 +1,10 @@ +module Invoiced + class TaxRate < Object + include Invoiced::Operations::List + include Invoiced::Operations::Create + include Invoiced::Operations::Update + include Invoiced::Operations::Delete + + OBJECT_NAME = 'tax_rate' + end +end \ No newline at end of file diff --git a/lib/invoiced/text_message.rb b/lib/invoiced/text_message.rb new file mode 100644 index 0000000..c699cca --- /dev/null +++ b/lib/invoiced/text_message.rb @@ -0,0 +1,5 @@ +module Invoiced + class TextMessage < Object + OBJECT_NAME = 'text_message' + end +end \ No newline at end of file diff --git a/lib/invoiced/transaction.rb b/lib/invoiced/transaction.rb index 4831fbe..52c72d2 100644 --- a/lib/invoiced/transaction.rb +++ b/lib/invoiced/transaction.rb @@ -20,5 +20,11 @@ def refund(params={}, opts={}) Util.convert_to_object(self, response[:body]) end + + def initiate_charge(params={}, opts={}) + response = @client.request(:post, "/charges", params, opts) + + Util.convert_to_object(self, response[:body]) + end end end \ No newline at end of file diff --git a/test/invoiced/coupon_test.rb b/test/invoiced/coupon_test.rb new file mode 100644 index 0000000..583e3c7 --- /dev/null +++ b/test/invoiced/coupon_test.rb @@ -0,0 +1,90 @@ +require File.expand_path('../../test_helper', __FILE__) + +module Invoiced + class CouponTest < Test::Unit::TestCase + should "return the api endpoint" do + coupon = Coupon.new(@client, "test") + assert_equal('/coupons/test', coupon.endpoint()) + end + + should "create a coupon" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test Coupon"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + coupon = @client.Coupon.create({:name => "Test Coupon"}) + + assert_instance_of(Invoiced::Coupon, coupon) + assert_equal("test", coupon.id) + assert_equal('Test Coupon', coupon.name) + end + + should "retrieve a coupon" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test Coupon"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + coupon = @client.Coupon.retrieve("test") + + assert_instance_of(Invoiced::Coupon, coupon) + assert_equal("test", coupon.id) + assert_equal('Test Coupon', coupon.name) + end + + should "not update a coupon when no params" do + coupon = Coupon.new(@client, "test") + assert_false(coupon.save) + end + + should "update a coupon" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"Testing Coupon"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + coupon = Coupon.new(@client, "test") + coupon.name = "Testing Coupon" + assert_true(coupon.save) + + assert_equal(coupon.name, "Testing Coupon") + end + + should "list all coupons" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":"test","name":"Test Coupon"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + coupons, metadata = @client.Coupon.list + + assert_instance_of(Array, coupons) + assert_equal(1, coupons.length) + assert_equal("test", coupons[0].id) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "delete a coupon" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(204) + mockResponse.stubs(:body).returns('') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + coupon = Coupon.new(@client, "test") + assert_true(coupon.delete) + end + end +end \ No newline at end of file diff --git a/test/invoiced/credit_note_test.rb b/test/invoiced/credit_note_test.rb index 1a85fc5..50cb030 100644 --- a/test/invoiced/credit_note_test.rb +++ b/test/invoiced/credit_note_test.rb @@ -122,5 +122,19 @@ class CreditNoteTest < Test::Unit::TestCase assert_instance_of(Invoiced::List, metadata) assert_equal(10, metadata.total_count) end + + should "void a credit note" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":123,"status":"voided"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + credit_note = CreditNote.new(@client, 123) + assert_true(credit_note.void) + + assert_equal(credit_note.status, 'voided') + end end end \ No newline at end of file diff --git a/test/invoiced/customer_test.rb b/test/invoiced/customer_test.rb index 02b2269..f4d8b76 100644 --- a/test/invoiced/customer_test.rb +++ b/test/invoiced/customer_test.rb @@ -87,7 +87,7 @@ class CustomerTest < Test::Unit::TestCase assert_true(customer.delete) end - should "send an account statement" do + should "send an account statement by email" do mockResponse = mock('RestClient::Response') mockResponse.stubs(:code).returns(201) mockResponse.stubs(:body).returns('[{"id":4567,"email":"test@example.com"}]') @@ -104,6 +104,40 @@ class CustomerTest < Test::Unit::TestCase assert_equal(4567, emails[0].id) end + should "send an account statement by text message" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('[{"id":5678,"state":"sent"}]') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + customer = Customer.new(@client, 123) + text_messages = customer.send_statement_sms(:type => "open_item", :message => "example") + + assert_instance_of(Array, text_messages) + assert_equal(1, text_messages.length) + assert_instance_of(Invoiced::TextMessage, text_messages[0]) + assert_equal(5678, text_messages[0].id) + end + + should "send an account statement by letter" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('[{"id":6789,"state":"queued"}]') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + customer = Customer.new(@client, 123) + letters = customer.send_statement_letter(:type => "open_item") + + assert_instance_of(Array, letters) + assert_equal(1, letters.length) + assert_instance_of(Invoiced::Letter, letters[0]) + assert_equal(6789, letters[0].id) + end + should "retrieve a customer's balance" do mockResponse = mock('RestClient::Response') mockResponse.stubs(:code).returns(200) @@ -141,7 +175,7 @@ class CustomerTest < Test::Unit::TestCase assert_equal('/customers/456/contacts/123', contact.endpoint()) end - should "list all of the customer's contact" do + should "list all of the customer's contacts" do mockResponse = mock('RestClient::Response') mockResponse.stubs(:code).returns(200) mockResponse.stubs(:body).returns('[{"id":123,"name":"Nancy"}]') @@ -246,5 +280,40 @@ class CustomerTest < Test::Unit::TestCase assert_instance_of(Invoiced::Invoice, invoice) assert_equal(4567, invoice.id) end + + should "list all the customer's notes" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":1212,"notes":"example"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + customer = Customer.new(@client, 123) + notes, metadata = customer.list_notes.list + + assert_instance_of(Array, notes) + assert_equal(1, notes.length) + assert_equal(1212, notes[0].id) + assert_equal('/customers/123/notes/1212', notes[0].endpoint()) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "create a consolidated invoice" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id": 999,"total":1000}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + customer = Customer.new(@client, 123) + invoice = customer.consolidate_invoices + + assert_instance_of(Invoiced::Invoice, invoice) + assert_equal(invoice.id, 999) + end end end \ No newline at end of file diff --git a/test/invoiced/estimate_test.rb b/test/invoiced/estimate_test.rb index d3b06cb..045926d 100644 --- a/test/invoiced/estimate_test.rb +++ b/test/invoiced/estimate_test.rb @@ -137,5 +137,19 @@ class EstimateTest < Test::Unit::TestCase assert_instance_of(Invoiced::List, metadata) assert_equal(10, metadata.total_count) end + + should "void an estimate" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":123,"status":"voided"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + estimate = Estimate.new(@client, 123) + assert_true(estimate.void) + + assert_equal(estimate.status, 'voided') + end end end \ No newline at end of file diff --git a/test/invoiced/invoice_test.rb b/test/invoiced/invoice_test.rb index 7e02f3c..bb252aa 100644 --- a/test/invoiced/invoice_test.rb +++ b/test/invoiced/invoice_test.rb @@ -87,7 +87,7 @@ class InvoiceTest < Test::Unit::TestCase assert_true(invoice.delete) end - should "send an invoice" do + should "send an invoice by email" do mockResponse = mock('RestClient::Response') mockResponse.stubs(:code).returns(201) mockResponse.stubs(:body).returns('[{"id":4567,"email":"test@example.com"}]') @@ -104,6 +104,40 @@ class InvoiceTest < Test::Unit::TestCase assert_equal(4567, emails[0].id) end + should "send an invoice by text message" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('[{"id":5678,"state":"sent"}]') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + invoice = Invoice.new(@client, 1234) + text_messages = invoice.send_sms(:message => "example") + + assert_instance_of(Array, text_messages) + assert_equal(1, text_messages.length) + assert_instance_of(Invoiced::TextMessage, text_messages[0]) + assert_equal(5678, text_messages[0].id) + end + + should "send an invoice by letter" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('[{"id":6789,"state":"queued"}]') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + invoice = Invoice.new(@client, 1234) + letters = invoice.send_letter + + assert_instance_of(Array, letters) + assert_equal(1, letters.length) + assert_instance_of(Invoiced::Letter, letters[0]) + assert_equal(6789, letters[0].id) + end + should "pay an invoice" do mockResponse = mock('RestClient::Response') mockResponse.stubs(:code).returns(200) @@ -170,5 +204,39 @@ class InvoiceTest < Test::Unit::TestCase assert_equal("active", payment_plan.status) assert_equal('/invoices/456/payment_plan', payment_plan.endpoint()) end + + should "list all notes associated with invoice" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":1212,"notes":"example"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + invoice = Invoice.new(@client, 1234) + notes, metadata = invoice.notes.list + + assert_instance_of(Array, notes) + assert_equal(1, notes.length) + assert_equal(1212, notes[0].id) + assert_equal('/invoices/1234/notes/1212', notes[0].endpoint()) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "void an invoice" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":123,"status":"voided"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + invoice = Invoice.new(@client, 123) + assert_true(invoice.void) + + assert_equal(invoice.status, 'voided') + end end end \ No newline at end of file diff --git a/test/invoiced/note_test.rb b/test/invoiced/note_test.rb new file mode 100644 index 0000000..dee8e07 --- /dev/null +++ b/test/invoiced/note_test.rb @@ -0,0 +1,90 @@ +require File.expand_path('../../test_helper', __FILE__) + +module Invoiced + class NoteTest < Test::Unit::TestCase + should "return the api endpoint" do + note = Note.new(@client, "test") + assert_equal('/notes/test', note.endpoint()) + end + + should "create a note" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id":"test","notes":"Test Note"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + note = @client.Note.create({:notes => "Test Note"}) + + assert_instance_of(Invoiced::Note, note) + assert_equal("test", note.id) + assert_equal('Test Note', note.notes) + end + + should "retrieve a note" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","notes":"Test Note"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + note = @client.Note.retrieve("test") + + assert_instance_of(Invoiced::Note, note) + assert_equal("test", note.id) + assert_equal('Test Note', note.notes) + end + + should "not update a note when no params" do + note = Note.new(@client, "test") + assert_false(note.save) + end + + should "update a note" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","notes":"new contents"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + note = Note.new(@client, "test") + note.notes = "new contents" + assert_true(note.save) + + assert_equal(note.notes, 'new contents') + end + + should "list all notes" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":"test","notes":"Test Note"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + notes, metadata = @client.Note.list + + assert_instance_of(Array, notes) + assert_equal(1, notes.length) + assert_equal("test", notes[0].id) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "delete a note" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(204) + mockResponse.stubs(:body).returns('') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + note = Note.new(@client, "test") + assert_true(note.delete) + end + end +end \ No newline at end of file diff --git a/test/invoiced/subscription_test.rb b/test/invoiced/subscription_test.rb index 0753358..cfe31f1 100644 --- a/test/invoiced/subscription_test.rb +++ b/test/invoiced/subscription_test.rb @@ -87,5 +87,18 @@ class SubscriptionTest < Test::Unit::TestCase assert_true(subscription.cancel) assert_equal("canceled", subscription.status) end + + should "preview a subscription" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"first_invoice": {"id": false}, "mrr": 9}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + subscription = Subscription.new(@client, 123) + preview = subscription.preview(:customer => 1234, :plan => "enterprise") + assert_equal(preview[:first_invoice], {:id=>false}) + end end end \ No newline at end of file diff --git a/test/invoiced/task_test.rb b/test/invoiced/task_test.rb new file mode 100644 index 0000000..d62ca18 --- /dev/null +++ b/test/invoiced/task_test.rb @@ -0,0 +1,90 @@ +require File.expand_path('../../test_helper', __FILE__) + +module Invoiced + class TaskTest < Test::Unit::TestCase + should "return the api endpoint" do + task = Task.new(@client, "test") + assert_equal('/tasks/test', task.endpoint()) + end + + should "create a task" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test Task"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + task = @client.Task.create({:name => "Test Task"}) + + assert_instance_of(Invoiced::Task, task) + assert_equal("test", task.id) + assert_equal('Test Task', task.name) + end + + should "retrieve a task" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test Task"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + task = @client.Task.retrieve("test") + + assert_instance_of(Invoiced::Task, task) + assert_equal("test", task.id) + assert_equal('Test Task', task.name) + end + + should "not update a task when no params" do + task = Task.new(@client, "test") + assert_false(task.save) + end + + should "update a task" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"new contents"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + task = Task.new(@client, "test") + task.name = "new contents" + assert_true(task.save) + + assert_equal(task.name, 'new contents') + end + + should "list all tasks" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":"test","name":"Test Task"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tasks, metadata = @client.Task.list + + assert_instance_of(Array, tasks) + assert_equal(1, tasks.length) + assert_equal("test", tasks[0].id) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "delete a task" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(204) + mockResponse.stubs(:body).returns('') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + task = Task.new(@client, "test") + assert_true(task.delete) + end + end +end \ No newline at end of file diff --git a/test/invoiced/tax_rate_test.rb b/test/invoiced/tax_rate_test.rb new file mode 100644 index 0000000..1029751 --- /dev/null +++ b/test/invoiced/tax_rate_test.rb @@ -0,0 +1,90 @@ +require File.expand_path('../../test_helper', __FILE__) + +module Invoiced + class TaxRateTest < Test::Unit::TestCase + should "return the api endpoint" do + tax_rate = TaxRate.new(@client, "test") + assert_equal('/tax_rates/test', tax_rate.endpoint()) + end + + should "create a tax rate" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test TaxRate"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tax_rate = @client.TaxRate.create({:name => "Test TaxRate"}) + + assert_instance_of(Invoiced::TaxRate, tax_rate) + assert_equal("test", tax_rate.id) + assert_equal('Test TaxRate', tax_rate.name) + end + + should "retrieve a tax rate" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"Test TaxRate"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tax_rate = @client.TaxRate.retrieve("test") + + assert_instance_of(Invoiced::TaxRate, tax_rate) + assert_equal("test", tax_rate.id) + assert_equal('Test TaxRate', tax_rate.name) + end + + should "not update a tax rate when no params" do + tax_rate = TaxRate.new(@client, "test") + assert_false(tax_rate.save) + end + + should "update a tax rate" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('{"id":"test","name":"new contents"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tax_rate = TaxRate.new(@client, "test") + tax_rate.name = "new contents" + assert_true(tax_rate.save) + + assert_equal(tax_rate.name, 'new contents') + end + + should "list all tax rates" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(200) + mockResponse.stubs(:body).returns('[{"id":"test","name":"Test TaxRate"}]') + mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '; rel="self", ; rel="first", ; rel="last"') + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tax_rates, metadata = @client.TaxRate.list + + assert_instance_of(Array, tax_rates) + assert_equal(1, tax_rates.length) + assert_equal("test", tax_rates[0].id) + + assert_instance_of(Invoiced::List, metadata) + assert_equal(15, metadata.total_count) + end + + should "delete a tax rate" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(204) + mockResponse.stubs(:body).returns('') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + tax_rate = TaxRate.new(@client, "test") + assert_true(tax_rate.delete) + end + end +end \ No newline at end of file diff --git a/test/invoiced/transaction_test.rb b/test/invoiced/transaction_test.rb index 181937a..d300891 100644 --- a/test/invoiced/transaction_test.rb +++ b/test/invoiced/transaction_test.rb @@ -118,5 +118,20 @@ class TransactionTest < Test::Unit::TestCase assert_instance_of(Invoiced::Transaction, refund) assert_equal(456, refund.id) end + + should "initiate a charge" do + mockResponse = mock('RestClient::Response') + mockResponse.stubs(:code).returns(201) + mockResponse.stubs(:body).returns('{"id":"a1b2c3","amount":100,"object":"card"}') + mockResponse.stubs(:headers).returns({}) + + RestClient::Request.any_instance.expects(:execute).returns(mockResponse) + + transaction = Transaction.new(@client, 1234) + charge = transaction.initiate_charge(:amount => 100, :payment_source_type => "card") + + assert_instance_of(Invoiced::Transaction, charge) + assert_equal(charge.id, "a1b2c3") + end end end \ No newline at end of file