From 3d09b67f3937bcf885872839922050d810d251e8 Mon Sep 17 00:00:00 2001 From: halorrr Date: Tue, 30 Jan 2024 19:53:07 -0500 Subject: [PATCH] finish remaining category endpoints --- lib/lunchmoney/categories/category_calls.rb | 10 +- .../add_to_category_group_success.yml | 56 +++++++++ .../create_category_group_success.yml | 55 +++++++++ .../categories/create_category_success.yml | 55 +++++++++ .../categories/delete_category_success.yml | 53 ++++++++ .../force_delete_category_success.yml | 53 ++++++++ .../categories/update_category_success.yml | 55 +++++++++ .../categories/category_calls_test.rb | 116 ++++++++++++++++++ 8 files changed, 447 insertions(+), 6 deletions(-) create mode 100644 test/cassettes/categories/add_to_category_group_success.yml create mode 100644 test/cassettes/categories/create_category_group_success.yml create mode 100644 test/cassettes/categories/create_category_success.yml create mode 100644 test/cassettes/categories/delete_category_success.yml create mode 100644 test/cassettes/categories/force_delete_category_success.yml create mode 100644 test/cassettes/categories/update_category_success.yml diff --git a/lib/lunchmoney/categories/category_calls.rb b/lib/lunchmoney/categories/category_calls.rb index 5109cd4..c34b88f 100644 --- a/lib/lunchmoney/categories/category_calls.rb +++ b/lib/lunchmoney/categories/category_calls.rb @@ -55,12 +55,11 @@ def category(category_id) exclude_from_totals: T::Boolean, archived: T::Boolean, group_id: T.nilable(Integer), - ).returns(T.any(T::Hash[String, Integer], LunchMoney::Errors)) + ).returns(T.any(T::Hash[Symbol, Integer], LunchMoney::Errors)) end def create_category(name:, description: nil, is_income: false, exclude_from_budget: false, exclude_from_totals: false, archived: false, group_id: nil) - - params = { + params = clean_params({ name:, description:, is_income:, @@ -68,8 +67,7 @@ def create_category(name:, description: nil, is_income: false, exclude_from_budg exclude_from_totals:, archived:, group_id:, - } - + }) response = post("categories", params) api_errors = errors(response) @@ -87,7 +85,7 @@ def create_category(name:, description: nil, is_income: false, exclude_from_budg exclude_from_totals: T::Boolean, category_ids: T::Array[Integer], new_categories: T::Array[String], - ).returns(T.any(T::Hash[String, Integer], LunchMoney::Errors)) + ).returns(T.any(T::Hash[Symbol, Integer], LunchMoney::Errors)) end def create_category_group(name:, description: nil, is_income: false, exclude_from_budget: false, exclude_from_totals: false, category_ids: [], new_categories: []) diff --git a/test/cassettes/categories/add_to_category_group_success.yml b/test/cassettes/categories/add_to_category_group_success.yml new file mode 100644 index 0000000..b934338 --- /dev/null +++ b/test/cassettes/categories/add_to_category_group_success.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: post + uri: https://dev.lunchmoney.app/v1/categories/group/784588/add + body: + encoding: UTF-8 + string: '{"category_ids":[],"new_categories":["New Category Test"]}' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706650303&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=0EJxT0Zygwu%2F6tVgKcP0vebiDwOlqiBcsnxNl02RVBY%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706650303&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=0EJxT0Zygwu%2F6tVgKcP0vebiDwOlqiBcsnxNl02RVBY%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '359' + Etag: + - W/"167-GLJvrW2cDAh4fTOKdZPUVe9uWVo" + Date: + - Tue, 30 Jan 2024 21:31:43 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: '{"id":784588,"name":"Create Category Group Test","description":null,"is_income":false,"exclude_from_budget":false,"exclude_from_totals":false,"is_group":true,"group_id":null,"children":[{"id":784589,"name":"New + Category Test","description":null,"created_at":"2024-01-30T21:31:43.878Z","archived":false,"archived_on":null}],"archived":false,"archived_on":null}' + recorded_at: Tue, 30 Jan 2024 21:31:43 GMT +recorded_with: VCR 6.2.0 diff --git a/test/cassettes/categories/create_category_group_success.yml b/test/cassettes/categories/create_category_group_success.yml new file mode 100644 index 0000000..9199a61 --- /dev/null +++ b/test/cassettes/categories/create_category_group_success.yml @@ -0,0 +1,55 @@ +--- +http_interactions: +- request: + method: post + uri: https://dev.lunchmoney.app/v1/categories/group + body: + encoding: UTF-8 + string: '{"name":"Create Category Group Test","description":null,"is_income":false,"exclude_from_budget":false,"exclude_from_totals":false,"category_ids":[],"new_categories":[]}' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706649809&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=vJ%2FT4jHTh2%2FqsjQBNSH0jta4kHOeFtDPmABZG2bBL8Q%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706649809&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=vJ%2FT4jHTh2%2FqsjQBNSH0jta4kHOeFtDPmABZG2bBL8Q%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '22' + Etag: + - W/"16-n+83QPrtwcQpPOQLBH0K5LK6sbM" + Date: + - Tue, 30 Jan 2024 21:23:29 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: '{"category_id":784588}' + recorded_at: Tue, 30 Jan 2024 21:23:29 GMT +recorded_with: VCR 6.2.0 diff --git a/test/cassettes/categories/create_category_success.yml b/test/cassettes/categories/create_category_success.yml new file mode 100644 index 0000000..8cb7911 --- /dev/null +++ b/test/cassettes/categories/create_category_success.yml @@ -0,0 +1,55 @@ +--- +http_interactions: +- request: + method: post + uri: https://dev.lunchmoney.app/v1/categories + body: + encoding: UTF-8 + string: '{"name":"Create Category Test","description":null,"is_income":false,"exclude_from_budget":false,"exclude_from_totals":false,"archived":false,"group_id":null}' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706649623&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=iKIpCkEkhgLm1FJEfUEiFqhtkLeH%2FZy3xX23rESUDNk%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706649623&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=iKIpCkEkhgLm1FJEfUEiFqhtkLeH%2FZy3xX23rESUDNk%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '22' + Etag: + - W/"16-hy/zK1fZzGmJnr4NHsZqg8oxW8M" + Date: + - Tue, 30 Jan 2024 21:20:23 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: '{"category_id":784587}' + recorded_at: Tue, 30 Jan 2024 21:20:23 GMT +recorded_with: VCR 6.2.0 diff --git a/test/cassettes/categories/delete_category_success.yml b/test/cassettes/categories/delete_category_success.yml new file mode 100644 index 0000000..af2230b --- /dev/null +++ b/test/cassettes/categories/delete_category_success.yml @@ -0,0 +1,53 @@ +--- +http_interactions: +- request: + method: delete + uri: https://dev.lunchmoney.app/v1/categories/784587 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706662241&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=A2j5X%2BUOX1bx8PaUz7hNlhiTKnWNaZuymHs9vUvuFhw%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706662241&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=A2j5X%2BUOX1bx8PaUz7hNlhiTKnWNaZuymHs9vUvuFhw%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '4' + Etag: + - W/"4-X/5TO4MPCKAyY0ipFgr6/IraRNs" + Date: + - Wed, 31 Jan 2024 00:50:44 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: 'true' + recorded_at: Wed, 31 Jan 2024 00:50:44 GMT +recorded_with: VCR 6.2.0 diff --git a/test/cassettes/categories/force_delete_category_success.yml b/test/cassettes/categories/force_delete_category_success.yml new file mode 100644 index 0000000..8c8de6f --- /dev/null +++ b/test/cassettes/categories/force_delete_category_success.yml @@ -0,0 +1,53 @@ +--- +http_interactions: +- request: + method: delete + uri: https://dev.lunchmoney.app/v1/categories/784588/force + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706662316&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=lBrHJ9hoG8o2dFYLIuM0F5fvDh%2BGfE34vY%2FbEpCWDwk%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706662316&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=lBrHJ9hoG8o2dFYLIuM0F5fvDh%2BGfE34vY%2FbEpCWDwk%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '4' + Etag: + - W/"4-X/5TO4MPCKAyY0ipFgr6/IraRNs" + Date: + - Wed, 31 Jan 2024 00:51:57 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: 'true' + recorded_at: Wed, 31 Jan 2024 00:51:57 GMT +recorded_with: VCR 6.2.0 diff --git a/test/cassettes/categories/update_category_success.yml b/test/cassettes/categories/update_category_success.yml new file mode 100644 index 0000000..0cec51d --- /dev/null +++ b/test/cassettes/categories/update_category_success.yml @@ -0,0 +1,55 @@ +--- +http_interactions: +- request: + method: put + uri: https://dev.lunchmoney.app/v1/categories/784587 + body: + encoding: UTF-8 + string: '{"name":"Update Category Test"}' + headers: + User-Agent: + - Faraday v2.9.0 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - Cowboy + Report-To: + - '{"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1706650076&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=cQQFD%2FOb1LmULOuWNf%2BV1cigM1hI9Mar4EUCGO%2BRtFM%3D"}]}' + Reporting-Endpoints: + - heroku-nel=https://nel.heroku.com/reports?ts=1706650076&sid=1b10b0ff-8a76-4548-befa-353fc6c6c045&s=cQQFD%2FOb1LmULOuWNf%2BV1cigM1hI9Mar4EUCGO%2BRtFM%3D + Nel: + - '{"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}' + Connection: + - keep-alive + X-Powered-By: + - Express + Vary: + - Origin, Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '4' + Etag: + - W/"4-X/5TO4MPCKAyY0ipFgr6/IraRNs" + Date: + - Tue, 30 Jan 2024 21:27:56 GMT + Via: + - 1.1 vegur + body: + encoding: UTF-8 + string: 'true' + recorded_at: Tue, 30 Jan 2024 21:27:56 GMT +recorded_with: VCR 6.2.0 diff --git a/test/lunchmoney/categories/category_calls_test.rb b/test/lunchmoney/categories/category_calls_test.rb index 688013d..9258cbe 100644 --- a/test/lunchmoney/categories/category_calls_test.rb +++ b/test/lunchmoney/categories/category_calls_test.rb @@ -95,4 +95,120 @@ class CategoryCallsTest < ActiveSupport::TestCase end end end + + test "create_category returns an id of created category success response" do + VCR.use_cassette("categories/create_category_success") do + api_call = LunchMoney::CategoryCalls.new.create_category(name: "Create Category Test") + api_call = T.cast(api_call, T::Hash[Symbol, Integer]) + + assert_kind_of(Integer, api_call[:category_id]) + end + end + + test "create_category returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:post).returns(response) + + api_call = LunchMoney::CategoryCalls.new.create_category(name: "Create Category Test") + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end + + test "create_category_group returns anid of created category group on success response" do + VCR.use_cassette("categories/create_category_group_success") do + api_call = LunchMoney::CategoryCalls.new.create_category_group(name: "Create Category Group Test") + api_call = T.cast(api_call, T::Hash[Symbol, Integer]) + + assert_kind_of(Integer, api_call[:category_id]) + end + end + + test "create_category_group returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:post).returns(response) + + api_call = LunchMoney::CategoryCalls.new.create_category_group(name: "Create Category Group Test") + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end + + test "update_category returns a boolean on success response" do + VCR.use_cassette("categories/update_category_success") do + api_call = LunchMoney::CategoryCalls.new.update_category(784587, name: "Update Category Test") + + assert_includes([TrueClass, FalseClass], api_call.class) + end + end + + test "update_category returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:put).returns(response) + + api_call = LunchMoney::CategoryCalls.new.update_category(784587, name: "Update Category Test") + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end + + test "add_to_category_group returns a Category object on success response" do + VCR.use_cassette("categories/add_to_category_group_success") do + api_call = LunchMoney::CategoryCalls.new.add_to_category_group(784588, new_categories: ["New Category Test"]) + + assert_kind_of(LunchMoney::Category, api_call) + end + end + + test "add_to_category_group returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:post).returns(response) + + api_call = LunchMoney::CategoryCalls.new.add_to_category_group(784588, new_categories: ["New Category Test"]) + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end + + test "delete_category returns a boolean on success response" do + VCR.use_cassette("categories/delete_category_success") do + api_call = LunchMoney::CategoryCalls.new.delete_category(784587) + + assert_includes([TrueClass, FalseClass], api_call.class) + end + end + + test "delete_category returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:delete).returns(response) + + api_call = LunchMoney::CategoryCalls.new.delete_category(784587) + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end + + test "force_delete_category returns a boolean on success response" do + VCR.use_cassette("categories/force_delete_category_success") do + api_call = LunchMoney::CategoryCalls.new.force_delete_category(784588) + + assert_includes([TrueClass, FalseClass], api_call.class) + end + end + + test "force_delete_category returns an array of Error objects on error response" do + response = mock_faraday_lunchmoney_error_response + LunchMoney::CategoryCalls.any_instance.stubs(:delete).returns(response) + + api_call = LunchMoney::CategoryCalls.new.force_delete_category(784588) + + api_call.each do |error| + assert_kind_of(LunchMoney::Error, error) + end + end end