Skip to content

Commit

Permalink
Merge pull request #7 from tithely/IN-2429/use-basic-auth
Browse files Browse the repository at this point in the history
IN-2429 3.0.0 Remove Resource::User and authenticate methods for basic authentication
  • Loading branch information
shawnpyle authored May 20, 2024
2 parents b1f1490 + 9b6bb6d commit 40a0612
Show file tree
Hide file tree
Showing 16 changed files with 2,510 additions and 2,524 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.2] - 2024-04-03
## [3.0.0] - 2024-05-20 -

### Fixed

1. Use basic auth for all API requests and remove User. (IN-2429)

### Removed

1. Remove `Resource::User` and `Resource::User#authenticate`. (IN-2429)

## [2.0.2] - 2024-04-04 - https://github.com/tithely/faithteams-api/pull/5

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
faithteams-api (2.0.2)
faithteams-api (3.0.0)
activesupport (>= 6.1.7)
http (~> 5.1)

Expand Down
17 changes: 1 addition & 16 deletions lib/faithteams/api/v2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module V2
class Connection
# Specific base urls for different resources
ENDPOINT_BASE_URLS = {
"authenticate" => "https://app.faithteams.com/api/v2",
"batches" => "https://api-v2.faithteams.com",
"contributions" => "https://api-v2.faithteams.com",
"contributiontypes" => "https://api-v2.faithteams.com",
Expand Down Expand Up @@ -77,8 +76,6 @@ def request(method: :get, path:, params: {}, body: {})
response = http.get(url, params: params)
end
break if response.status != 401 || retries >= 2

authenticate
end

raise Error::Request.new(response: response, message: "Request unsuccessful (#{response.status})") unless response.status.success?
Expand Down Expand Up @@ -116,26 +113,14 @@ def user_resource

# @return [HTTP::Client]
def http
@http ||= HTTP.headers("Token" => "#{auth_token}")
@http ||= HTTP.basic_auth(user: user_id, pass: password)
end

# @param path [String]
# @return [String]
def base_url(path:)
ENDPOINT_BASE_URLS[path.split("/")[1]]
end

# Set the auth_token for these requests
# @return [String]
def auth_token
@auth_token ||= user_resource.authenticate
end

# Resets existing auth_token and re-authenticates
def authenticate
@auth_token = nil
auth_token
end
end
end
end
Expand Down
5 changes: 0 additions & 5 deletions lib/faithteams/api/v2/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def contribution
@contribution ||= Resource::Contribution.new(connection: connection)
end

# @return [Resource::User]
def user
@user ||= Resource::User.new(connection: connection)
end

# @return [Resource::ContributionType]
def contribution_type
@contribution_type ||= Resource::ContributionType.new(connection: connection)
Expand Down
1 change: 0 additions & 1 deletion lib/faithteams/api/v2/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require_relative "resource/fund"
require_relative "resource/person"
require_relative "resource/contribution"
require_relative "resource/user"
require_relative "resource/contribution_type"

module FaithTeams
Expand Down
40 changes: 0 additions & 40 deletions lib/faithteams/api/v2/resource/user.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/faithteams/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module FaithTeams
# Current version number.
VERSION = "2.0.2"
VERSION = "3.0.0"
end
21 changes: 1 addition & 20 deletions spec/faithteams/api/v2/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

it "is false if connection is not valid" do
stub_contribution_types(status: 401, body: any_json(status: 401))
stub_authenticate(status: 401, body: any_json(status: 401))
expect(connection.valid?).to be(false)
end
end
Expand Down Expand Up @@ -118,45 +117,29 @@

describe "request" do
context "when successful" do
before do
stub_authenticate
end

it "returns HTTP::Response" do
stub_funds
expect(connection.request(path: "/funds")).to be_a(HTTP::Response)
end
end

context "when authentication fails on non-authenticate request" do
context "when failure" do
before do
stub_funds(status: 401, body: any_json(status: 401))
stub_authenticate
end

it "will raise unathenticated error after multiple attempts" do
expect(connection.user_resource).to receive(:authenticate).exactly(2).times.and_call_original

expect { connection.request(path: "/funds") }.to raise_error(FaithTeams::API::V2::Error::Request, "Request unsuccessful (401 Unauthorized): Unauthorized")
end
end

context "when authentication fails on authenticate request" do
before do
stub_authenticate(status: 401, body: any_json(status: 401))
end

it "will raise unathenticated error immediately" do
expect(connection.user_resource).to receive(:authenticate).once.and_call_original

expect { connection.request(path: "/funds") }.to raise_error(FaithTeams::API::V2::Error::Request, "Request unsuccessful (401 Unauthorized): Unauthorized")
end
end

context "when not successful" do
before do
stub_funds(status: 511, body: "")
stub_authenticate
end

it "raises Error::Request" do
Expand Down Expand Up @@ -185,7 +168,6 @@

context "not successful" do
it "raises error if response is not 200" do
stub_authenticate
stub_funds(status: 500, body: any_json(status: 500))
expect { connection.request_and_parse(path: "/funds") }.to raise_error(FaithTeams::API::V2::Error::Request, "Request unsuccessful (500 Internal Server Error): Internal Server Error")
end
Expand All @@ -207,7 +189,6 @@

context "200 status but success = false" do
it "raises Error::Request" do
stub_authenticate
stub_contributions_find(parent_id: 30, description: "error")
expect { connection.request_and_parse(path: "/contributions/30") }.to raise_error(FaithTeams::API::V2::Error::Request, "Request unsuccessful: Error: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: '429955534343434'")
end
Expand Down
6 changes: 0 additions & 6 deletions spec/faithteams/api/v2/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
end
end

describe "user" do
it "is a Resource::User" do
expect(gateway.user).to be_instance_of(FaithTeams::API::V2::Resource::User)
end
end

describe "contribution_type" do
it "is a Resource::ContributionType" do
expect(gateway.contribution_type).to be_instance_of(FaithTeams::API::V2::Resource::ContributionType)
Expand Down
28 changes: 0 additions & 28 deletions spec/faithteams/api/v2/resource/user_spec.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/v2/stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def any_json(status:)
read_fixture(name: "any.#{status}.json")
end

def authenticate_json(status:)
read_fixture(name: "authenticate.#{status}.json")
end

def batches_json(status: 200, method: nil, description: nil)
name_json(resource: "batches", status: status, method: method, description: description)
end
Expand Down Expand Up @@ -40,7 +36,6 @@ def stub_contribution_type(id:, status: 200, body: nil)

def endpoint_base_urls
{
"authenticate" => "https://app.faithteams.com/api/v2",
"batches" => "https://api-v2.faithteams.com",
"contributions" => "https://api-v2.faithteams.com",
"contributiontypes" => "https://api-v2.faithteams.com",
Expand All @@ -63,10 +58,6 @@ def people_json(status: 200, method: nil, description: nil)
name_json(resource: "people", status: status, method: method, description: description)
end

def stub_authenticate(status: 200, body: nil)
stub_faithteams_request(method: :post, path: "/authenticate", params: {}, body: body || authenticate_json(status: status), status: status)
end

def stub_batches_create(status: 201, body: nil, method: "create", description: nil)
stub_faithteams_request(
method: :post,
Expand Down
Loading

0 comments on commit 40a0612

Please sign in to comment.