Skip to content

Commit

Permalink
Adds authenticated access_token request
Browse files Browse the repository at this point in the history
  • Loading branch information
dnstufff committed Nov 30, 2022
1 parent 9f1e239 commit 3c4c2d6
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/exponent-server-sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Client
def initialize(**args)
@http_client = args[:http_client] || Typhoeus
@error_builder = ErrorBuilder.new
@access_token = args[:access_token]
# future versions will deprecate this
@response_handler = args[:response_handler] || ResponseHandler.new
@gzip = args[:gzip] == true
Expand All @@ -48,7 +49,7 @@ def initialize(**args)
# @deprecated
def publish(messages)
warn '[DEPRECATION] `publish` is deprecated. Please use `send_messages` instead.'
@response_handler.handle(push_notifications(messages))
response_handler.handle(push_notifications(messages))
end

# returns response handler that provides access to errors? and other response inspection methods
Expand All @@ -73,12 +74,14 @@ def verify_deliveries(receipt_ids, **args)

private

attr_reader :http_client, :error_builder, :access_token, :response_handler, :gzip

def push_notifications(messages)
@http_client.post(
http_client.post(
push_url,
body: messages.to_json,
headers: headers,
accept_encoding: @gzip
accept_encoding: gzip
)
end

Expand All @@ -87,11 +90,11 @@ def push_url
end

def get_receipts(receipt_ids)
@http_client.post(
http_client.post(
receipts_url,
body: { ids: receipt_ids }.to_json,
headers: headers,
accept_encoding: @gzip
accept_encoding: gzip
)
end

Expand All @@ -100,11 +103,11 @@ def receipts_url
end

def headers
headers = {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
headers
{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': ("Bearer #{access_token}" if access_token)
}.compact
end
end

Expand Down
169 changes: 169 additions & 0 deletions test/exponent-server-sdk-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def setup
@response_mock = MiniTest::Mock.new
@client = Exponent::Push::Client.new(http_client: @mock)
@client_gzip = Exponent::Push::Client.new(http_client: @mock, gzip: true)
@access_token = 'EXPO_ACCESS_TOKEN'
@client_authenticated = Exponent::Push::Client.new(http_client: @mock, access_token: @access_token)
end

def test_send_messages_with_success
Expand Down Expand Up @@ -49,6 +51,18 @@ def test_send_messages_with_gzip_success
@mock.verify
end

def test_send_messages_with_authentication_success
@response_mock.expect(:code, 200)
@response_mock.expect(:body, success_body.to_json)

@mock.expect(:post, @response_mock, authenticated_client_args)

response = @client_authenticated.send_messages(messages)
assert_equal(response.errors?, false)

@mock.verify
end

def test_send_messages_with_empty_string_response_body
@response_mock.expect(:code, 400)
@response_mock.expect(:body, '')
Expand Down Expand Up @@ -112,6 +126,27 @@ def test_send_messages_with_gzip_empty_string_response
@mock.verify
end

def test_send_messages_with_authentication_empty_string_response
@response_mock.expect(:code, 400)
@response_mock.expect(:body, '')

@mock.expect(:post, @response_mock, authenticated_client_args)

exception = assert_raises Exponent::Push::UnknownError do
handler = @client_authenticated.send_messages(messages)
# this first assertion is just stating that errors will be false when
# an exception is thrown on the request, not the content of the request
# 400/500 level errors are not delivery errors, they are functionality errors
assert_equal(handler.response.errors?, false)
assert_equal(handler.response.body, {})
assert_equal(handler.response.code, 400)
end

assert_match(/Unknown error format/, exception.message)

@mock.verify
end

def test_send_messages_with_gzip_nil_response_body
@response_mock.expect(:code, 400)
@response_mock.expect(:body, nil)
Expand All @@ -133,6 +168,27 @@ def test_send_messages_with_gzip_nil_response_body
@mock.verify
end

def test_send_messages_with_authenticated_nil_response_body
@response_mock.expect(:code, 400)
@response_mock.expect(:body, nil)

@mock.expect(:post, @response_mock, authenticated_client_args)

exception = assert_raises Exponent::Push::UnknownError do
handler = @client_authenticated.send_messages(messages)
# this first assertion is just stating that errors will be false when
# an exception is thrown on the request, not the content of the request
# 400/500 level errors are not delivery errors, they are functionality errors
assert_equal(handler.response.errors?, false)
assert_equal(handler.response.body, {})
assert_equal(handler.response.code, 400)
end

assert_match(/Unknown error format/, exception.message)

@mock.verify
end

def test_send_messages_with_unknown_error
@response_mock.expect(:code, 400)
@response_mock.expect(:body, error_body.to_json)
Expand Down Expand Up @@ -163,6 +219,21 @@ def test_send_messages_with_gzip_unknown_error
@mock.verify
end

def test_send_messages_with_authenticated_unknown_error
@response_mock.expect(:code, 400)
@response_mock.expect(:body, error_body.to_json)

@mock.expect(:post, @response_mock, authenticated_client_args)

exception = assert_raises Exponent::Push::UnknownError do
@client_authenticated.send_messages(messages)
end

assert_match(/Unknown error format/, exception.message)

@mock.verify
end

def test_send_messages_with_device_not_registered_error
@response_mock.expect(:code, 200)
@response_mock.expect(:body, not_registered_device_error_body.to_json)
Expand Down Expand Up @@ -305,6 +376,19 @@ def test_get_receipts_with_gzip_success_receipt
@mock.verify
end

def test_get_receipts_with_authenticated_success_receipt
@response_mock.expect(:code, 200)
@response_mock.expect(:body, receipt_success_body.to_json)
receipt_ids = [success_receipt]

@mock.expect(:post, @response_mock, authenticated_receipt_client_args(receipt_ids))

response_handler = @client_authenticated.verify_deliveries(receipt_ids)
assert_match(success_receipt, response_handler.receipt_ids.first)

@mock.verify
end

def test_get_receipts_with_gzip_error_receipt
@response_mock.expect(:code, 200)
@response_mock.expect(:body, receipt_error_body.to_json)
Expand All @@ -321,6 +405,22 @@ def test_get_receipts_with_gzip_error_receipt
@mock.verify
end

def test_get_receipts_with_authenticated_error_receipt
@response_mock.expect(:code, 200)
@response_mock.expect(:body, receipt_error_body.to_json)
receipt_ids = [error_receipt]

@mock.expect(:post, @response_mock, authenticated_receipt_client_args(receipt_ids))

response_handler = @client_authenticated.verify_deliveries(receipt_ids)
assert_match(error_receipt, response_handler.receipt_ids.first)
assert_equal(true, response_handler.errors?)
assert_equal(1, response_handler.errors.count)
assert(response_handler.errors.first.instance_of?(Exponent::Push::DeviceNotRegisteredError))

@mock.verify
end

def test_get_receipts_with_gzip_variable_success_receipts
@response_mock.expect(:code, 200)
@response_mock.expect(:body, multiple_receipts.to_json)
Expand All @@ -338,6 +438,23 @@ def test_get_receipts_with_gzip_variable_success_receipts
@mock.verify
end

def test_get_receipts_with_authenticated_variable_success_receipts
@response_mock.expect(:code, 200)
@response_mock.expect(:body, multiple_receipts.to_json)
receipt_ids = [error_receipt, success_receipt]

@mock.expect(:post, @response_mock, authenticated_receipt_client_args(receipt_ids))

response_handler = @client_authenticated.verify_deliveries(receipt_ids)
assert_match(error_receipt, response_handler.receipt_ids.first)
assert_match(success_receipt, response_handler.receipt_ids.last)
assert_equal(true, response_handler.errors?)
assert_equal(1, response_handler.errors.count)
assert(response_handler.errors.first.instance_of?(Exponent::Push::DeviceNotRegisteredError))

@mock.verify
end

# DEPRECATED -- TESTS BELOW HERE RELATE TO CODE THAT WILL BE REMOVED

def test_publish_with_success
Expand All @@ -362,6 +479,17 @@ def test_publish_with_gzip_success
@mock.verify
end

def test_publish_authenticated_success
@response_mock.expect(:code, 200)
@response_mock.expect(:body, success_body.to_json)

@mock.expect(:post, @response_mock, authenticated_client_args)

@client_authenticated.publish(messages)

@mock.verify
end

def test_publish_with_gzip
@response_mock.expect(:code, 200)
@response_mock.expect(:body, success_body.to_json)
Expand All @@ -373,6 +501,17 @@ def test_publish_with_gzip
@mock.verify
end

def test_publish_authenticated
@response_mock.expect(:code, 200)
@response_mock.expect(:body, success_body.to_json)

@mock.expect(:post, @response_mock, authenticated_client_args)

@client_authenticated.publish(messages)

@mock.verify
end

def test_publish_with_unknown_error
@response_mock.expect(:code, 400)
@response_mock.expect(:body, error_body.to_json)
Expand Down Expand Up @@ -603,6 +742,21 @@ def gzip_client_args
]
end

def authenticated_client_args
[
'https://exp.host/--/api/v2/push/send',
{
body: messages.to_json,
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => "Bearer #{@access_token}"
},
accept_encoding: true
}
]
end

def receipt_client_args(receipt_ids)
[
'https://exp.host/--/api/v2/push/getReceipts',
Expand Down Expand Up @@ -631,6 +785,21 @@ def gzip_receipt_client_args(receipt_ids)
]
end

def authenticated_receipt_client_args(receipt_ids)
[
'https://exp.host/--/api/v2/push/getReceipts',
{
body: { ids: receipt_ids }.to_json,
headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => "Bearer #{@access_token}"
},
accept_encoding: true
}
]
end

def alternate_format_messages
[{
to: [
Expand Down

0 comments on commit 3c4c2d6

Please sign in to comment.