From e80de55f0ef9366aa7adccd68336a94eb7173615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joanna=20W=C3=B3jtowicz?= Date: Wed, 22 Jun 2022 15:25:28 +0200 Subject: [PATCH] Add associate_by_email_address method --- lib/urbanairship/devices/named_user.rb | 18 ++++++++ .../urbanairship/devices/named_user_spec.rb | 43 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/urbanairship/devices/named_user.rb b/lib/urbanairship/devices/named_user.rb index c086d72..7d47696 100644 --- a/lib/urbanairship/devices/named_user.rb +++ b/lib/urbanairship/devices/named_user.rb @@ -42,6 +42,24 @@ def associate(channel_id: required('channel_id'), device_type: nil) response end + def associate_by_email_address(email_address: required('email_address')) + fail ArgumentError, + 'named_user_id is required for association' if @named_user_id.nil? + + payload = {} + payload['email_address'] = email_address + payload['named_user_id'] = @named_user_id.to_s + + response = @client.send_request( + method: 'POST', + body: JSON.dump(payload), + path: named_users_path('associate'), + content_type: CONTENT_TYPE + ) + logger.info { "Associated email_address #{email_address} with named_user #{@named_user_id}" } + response + end + def disassociate(channel_id: required('channel_id'), device_type: nil) payload = {} payload['channel_id'] = channel_id diff --git a/spec/lib/urbanairship/devices/named_user_spec.rb b/spec/lib/urbanairship/devices/named_user_spec.rb index a0e267d..fca8282 100644 --- a/spec/lib/urbanairship/devices/named_user_spec.rb +++ b/spec/lib/urbanairship/devices/named_user_spec.rb @@ -7,6 +7,7 @@ let(:channel_id) { '123' } let(:device_type) { 'android' } let(:named_user_id) { 'user' } + let(:email_address) { 'whales@example.com' } named_user = nil @@ -112,6 +113,48 @@ end end + describe '#associate_by_email_address' do + describe 'Request' do + after(:each) { named_user.associate_by_email_address(email_address: email_address) } + + it 'makes the expected request' do + allow(airship).to receive(:send_request) do |arguments| + expect(arguments).to eq( + method: 'POST', + body: { email_address: email_address, named_user_id: named_user_id }.to_json, + path: "/named_users/associate", + content_type: described_class::CONTENT_TYPE, + ) + expected_response + end + end + + context 'Named user ID is an integer' do + let(:named_user_id) { 1985 } + + it 'converts named user ID to a string' do + allow(airship).to receive(:send_request) do |arguments| + expect(JSON.parse(arguments[:body], symbolize_names: true)[:named_user_id]).to eq named_user_id.to_s + expected_response + end + end + end + end + + it 'associates a email_address with a named_user' do + allow(airship).to receive(:send_request).and_return(expected_response) + actual_resp = named_user.associate_by_email_address(email_address: email_address) + expect(actual_resp).to eq(expected_response) + end + + it 'fails when the user_id is not set' do + named_user_without_id = UA::NamedUser.new(client: airship) + expect { + named_user_without_id.associate_by_email_address(email_address: email_address) + }.to raise_error(ArgumentError) + end + end + describe '#disassociate' do it 'disassociates a channel from a named_user' do allow(airship).to receive(:send_request).and_return(expected_response)