Skip to content

Commit

Permalink
Fix failing tests, add for unsubuscribed_at
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Nov 3, 2023
1 parent fcd391c commit cb0cb9e
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/adapters/signal_adapter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def perform_request(request, contributor)
yield response if block_given?
else
error_message = JSON.parse(response.body)['error']
mark_contributor_as_inactive(contributor) if error_message.match?(/User is not registered/)
mark_contributor_as_inactive(contributor) if error_message.match?(/Unregistered user/)
exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message)
context = {
code: response.code,
Expand Down
2 changes: 1 addition & 1 deletion app/adapters/signal_adapter/outbound/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def perform(message:)
'Content-Type': 'application/json'
})
request.body = data.to_json
SignalAdapter::Api.perform_request(request, recipient) do
SignalAdapter::Api.perform_request(request, message.recipient) do
# TODO: Do something on success. For example, mark the message as delivered?
# Or should we use deliver receipts as the source of truth.
Rails.logger.debug 'Great!'
Expand Down
4 changes: 3 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ de:
action: Mitglied reaktivieren
automatically_marked_inactive:
text: "<strong>%{name}</strong> ist ein <strong>inaktives Mitglied</strong> deiner Community und erhält aktuell keine Recherchefragen über %{project_name}."
possible_reason_whats_app: "Die Rufnummer wurde möglicherweise nicht bei WhatsApp registriert. Bitte überprüfe dies mit <strong>%{first_name}</strong>."
possible_reason_whats_app: |
Die Rufnummer wurde möglicherweise nicht bei WhatsApp registriert oder der Empfänger hat möglicherweise die neuen Nutzungsbedingungen und Datenschutzrichtlinien von WhatsApp nicht akzeptiert.
Es ist auch möglich, dass der Empfänger verwendet eine alte, nicht unterstützte Version des WhatsApp-Clients für sein Telefon. Bitte überprüfe dies mit <strong>%{first_name}</strong>.
possible_reason_signal: "Die Rufnummer wurde möglicherweise nicht bei Signal registriert. Bitte überprüfe dies mit <strong>%{first_name}</strong>."
possible_reason_threema: "Diese Threema-ID wurde möglicherweise von Threema als ungültig gemeldet. Bitte <strong>%{first_name}</strong>, die Threema-ID zu überprüfen."
possible_reason_email: "Diese E-Mail-Adresse wurde vom Server als inaktiv gemeldet. Das bedeutet, es können keine E-Mails an diese Adresse zugestellt werden."
Expand Down
4 changes: 2 additions & 2 deletions spec/adapters/signal_adapter/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
it { should have_received(:report) }
end

describe 'User is not registered error' do
describe 'Unregistered user error' do
let!(:admin) { create_list(:user, 2, admin: true) }
let!(:non_admin_user) { create(:user) }

before do
stub_request(:post, uri).to_return(status: 400, body: { error: 'User is not registered' }.to_json)
stub_request(:post, uri).to_return(status: 400, body: { error: 'Unregistered user' }.to_json)
end

subject { -> { api.perform_request(request, recipient) } }
Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/signal_adapter/outbound/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

describe 'on error' do
let(:error_message) { 'User is not registered' }
let(:error_message) { 'Unregistered user' }
before(:each) { stub_request(:post, 'http://signal:8080/v2/send').to_return(status: 400, body: { error: error_message }.to_json) }

it 'reports the error' do
Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/signal_adapter/outbound/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

describe 'on error' do
let(:error_message) { 'User is not registered' }
let(:error_message) { 'Unregistered user' }
before(:each) { stub_request(:post, 'http://signal:8080/v2/send').to_return(status: 400, body: { error: error_message }.to_json) }

it 'reports the error' do
Expand Down
4 changes: 2 additions & 2 deletions spec/components/contributor_status_toggle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
end

context 'through WhatsApp who requested to unsubscribe' do
before { contributor.update(whats_app_phone_number: '+49151234567', email: nil) }
before { contributor.update(whats_app_phone_number: '+49151234567', email: nil, unsubscribed_at: 1.minute.ago) }

it { should have_css('strong', text: contributor.first_name) }
it { should have_content('hat darum gebeten, vom Empfang von Nachrichten über WhatsApp abgemeldet zu werden.') }
it { should have_content('hat darum gebeten, vom Erhalt von Nachrichten über WhatsApp abgemeldet zu werden.') }
end
end
end
28 changes: 20 additions & 8 deletions spec/models/contributor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,9 @@
subject { Contributor.active }

context 'given some inactive and active contributors' do
let(:active_contributor) { create(:contributor, active: true) }
let(:inactive_contributor) { create(:contributor, active: false) }

before { active_contributor && inactive_contributor }
let!(:active_contributor) { create(:contributor, deactivated_at: nil) }
let!(:unsubscribed_contributor) { create(:contributor, unsubscribed_at: 1.day.ago) }
let!(:inactive_contributor) { create(:contributor, deactivated_at: 1.hour.ago) }

it 'returns only active contributors' do
should eq([active_contributor])
Expand All @@ -628,17 +627,30 @@
subject { Contributor.inactive }

context 'given some inactive and active contributors' do
let(:active_contributor) { create(:contributor, active: true) }
let(:inactive_contributor) { create(:contributor, active: false) }

before { active_contributor && inactive_contributor }
let!(:active_contributor) { create(:contributor, deactivated_at: nil) }
let!(:unsubscribed_contributor) { create(:contributor, unsubscribed_at: 1.day.ago) }
let!(:inactive_contributor) { create(:contributor, deactivated_at: 1.hour.ago) }

it 'returns only inactive contributors' do
should eq([inactive_contributor])
end
end
end

describe 'scope ::unsubscribed' do
subject { Contributor.unsubscribed }

context 'given some inactive and active contributors' do
let!(:active_contributor) { create(:contributor, deactivated_at: nil) }
let!(:unsubscribed_contributor) { create(:contributor, unsubscribed_at: 1.day.ago) }
let!(:inactive_contributor) { create(:contributor, deactivated_at: 1.hour.ago) }

it 'returns only inactive contributors' do
should eq([unsubscribed_contributor])
end
end
end

describe '.active' do
subject { contributor.active }
it { should be(true) }
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/whats_app/three_sixty_dialog_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
end

it 'marks contributor as inactive' do
expect { subject.call }.to change { contributor.reload.deactivated_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
expect { subject.call }.to change { contributor.reload.unsubscribed_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
end

it 'enqueues a job to inform the contributor of successful unsubscribe' do
Expand Down Expand Up @@ -210,7 +210,7 @@
let!(:non_admin_user) { create(:user) }

before do
contributor.update(deactivated_at: Time.current)
contributor.update(unsubscribed_at: Time.current)
params[:messages].first[:text][:body] = 'Bestellen'
end

Expand All @@ -219,7 +219,7 @@
end

it 'marks contributor as active' do
expect { subject.call }.to change { contributor.reload.deactivated_at }.from(kind_of(ActiveSupport::TimeWithZone)).to(nil)
expect { subject.call }.to change { contributor.reload.unsubscribed_at }.from(kind_of(ActiveSupport::TimeWithZone)).to(nil)
end

it 'marks that contributor has responded to template message' do
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@
"_#{I18n.t('adapter.whats_app.subscribe.instructions')}_"].join("\n\n") }
end

it 'marks contributor as inactive' do
expect { subject.call }.to change { contributor.reload.deactivated_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
it 'marks contributor as unsubscribed' do
expect { subject.call }.to change { contributor.reload.unsubscribed_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
end

it 'enqueues a job to inform the contributor of successful unsubscribe' do
Expand Down Expand Up @@ -212,7 +212,7 @@
let!(:non_admin_user) { create(:user) }

before do
contributor.update(deactivated_at: Time.current)
contributor.update(unsubscribed_at: Time.current)
params['Body'] = 'Bestellen'
end

Expand All @@ -221,7 +221,7 @@
end

it 'marks contributor as active' do
expect { subject.call }.to change { contributor.reload.deactivated_at }.from(kind_of(ActiveSupport::TimeWithZone)).to(nil)
expect { subject.call }.to change { contributor.reload.unsubscribed_at }.from(kind_of(ActiveSupport::TimeWithZone)).to(nil)
end

it 'marks that contributor has responded to template message' do
Expand Down

0 comments on commit cb0cb9e

Please sign in to comment.