From e1bdb1324f43ac15991795fd1db55ba60b1e4ee1 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Sat, 10 Dec 2022 04:21:37 +0100 Subject: [PATCH] make the export worker create a *real* notification and add specs for it --- app/models/notification/data_exported.rb | 5 ++++ app/models/user/data_export.rb | 5 ++++ .../notifications/type/_dataexport.html.haml | 8 +++++ app/workers/export_worker.rb | 20 +++++++++---- config/locales/views.en.yml | 6 +++- spec/workers/export_worker_spec.rb | 29 +++++++++++++++++++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 app/models/notification/data_exported.rb create mode 100644 app/models/user/data_export.rb create mode 100644 app/views/notifications/type/_dataexport.html.haml create mode 100644 spec/workers/export_worker_spec.rb diff --git a/app/models/notification/data_exported.rb b/app/models/notification/data_exported.rb new file mode 100644 index 00000000..dc9d1634 --- /dev/null +++ b/app/models/notification/data_exported.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# NOTE: `target` is not really used for this notification, but it should be of type `User::DataExport` +class Notification::DataExported < Notification +end diff --git a/app/models/user/data_export.rb b/app/models/user/data_export.rb new file mode 100644 index 00000000..c8aee5e9 --- /dev/null +++ b/app/models/user/data_export.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# stub model to nicely allow for data export notifications, do NOT use this directly! +class User::DataExport < User +end diff --git a/app/views/notifications/type/_dataexport.html.haml b/app/views/notifications/type/_dataexport.html.haml new file mode 100644 index 00000000..b39210fe --- /dev/null +++ b/app/views/notifications/type/_dataexport.html.haml @@ -0,0 +1,8 @@ +.media.notification + .notification__icon + %i.fa.fa-2x.fa-fw.fa-download + .media-body + %h6.media-heading.notification__user + = t(".heading") + .notification__text + = t(".text_html", settings_export: link_to(t(".settings_export"), settings_export_path)) diff --git a/app/workers/export_worker.rb b/app/workers/export_worker.rb index 420ee06e..411c5318 100644 --- a/app/workers/export_worker.rb +++ b/app/workers/export_worker.rb @@ -1,4 +1,7 @@ -require 'exporter' +# frozen_string_literal: true + +require "exporter" + class ExportWorker include Sidekiq::Worker @@ -6,11 +9,16 @@ class ExportWorker # @param user_id [Integer] the user id def perform(user_id) - exporter = Exporter.new User.find(user_id) + user = User.find(user_id) + + exporter = Exporter.new(user) exporter.export - question = Question.create(content: "Your #{APP_CONFIG['site_name']} data export is ready! You can download it " + - "from the settings page under the \"Export\" tab.", author_is_anonymous: true, - author_identifier: "retrospring_exporter") - Inbox.create(user_id: user_id, question_id: question.id, new: true) + + Notification::DataExported.create( + target_id: user.id, + target_type: "User::DataExport", + recipient: user, + new: true + ) end end diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index cfe1c84d..f419a71a 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -327,6 +327,10 @@ en: link_text: "their answer" other: link_text_html: "%{user}'s answer" + dataexport: + heading: "Your data export is ready" + text_html: "Head over to %{settings_export} to download it." + settings_export: "the settings page" reaction: heading_html: "%{user} smiled %{type} %{time} ago" answer: @@ -396,7 +400,7 @@ en:

The data is inside a .zip archive that contains some JSON files. The archive also contains a copy of your profile picture and header picture in all sizes.

Please note that you can only export your data once a week. Exporting your data - will take a while, so please be patient. You will receive a question once exporting + will take a while, so please be patient. You will receive a notification once exporting is done.

export: "Export" export_url: diff --git a/spec/workers/export_worker_spec.rb b/spec/workers/export_worker_spec.rb new file mode 100644 index 00000000..71935d95 --- /dev/null +++ b/spec/workers/export_worker_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ExportWorker do + let(:user) { FactoryBot.create(:user) } + + describe "#perform" do + let(:exporter_double) { double("Exporter") } + + before do + # stub away the testing of the exporter itself since it is done in lib/exporter_spec + allow(exporter_double).to receive(:export) + allow(Exporter).to receive(:new).and_return(exporter_double) + end + + subject { described_class.new.perform(user.id) } + + it "creates an exported notification" do + expect { subject }.to change { Notification::DataExported.count }.by(1) + + notification = Notification::DataExported.last + expect(notification.target_id).to eq(user.id) + expect(notification.target_type).to eq("User::DataExport") + expect(notification.recipient).to eq(user) + expect(notification.new).to be true + end + end +end