Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for the cards_update check #54

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/tasks/cards_update_check.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,42 @@ namespace :cards_update do
# | 5 | 180 + 14*4 + 60 |
# | 5 +i | 180 + 14*4 + 60 * (i+1) | // send every two months

# to be fair we cap the last_updated to the release of V2 - 6 months
# to be adjusted so that when we release the task we quickly get 180 days
release = Date.strptime("1/6/2021", "%d/%m/%Y")
# to be fair we cap the last_updated to the release of V2 - 6 months to be
# adjusted so that when we release the task we quickly get 180 days. To be
# removed once all cards have received their warnings.
release = Date.strptime("4/7/2021", "%d/%m/%Y")

if Rails.env == "test"
release = Date.strptime("1/1/1970", "%d/%m/%Y")
end

base = [release, card.last_updated.to_date].max

days_from_update = (Time.now.to_date - base).to_i
days_from_update = (Time.current.to_date - base).to_i

days2step = {180 => 0, 180+14 => 1, 180+14*2 => 2, 180+14*3 => 3, 180+14*4 => 4}

step = days2step[days_from_update] || -1

if step == -1 && days_from_update > 180 + 14*4 && (days_from_update - (180 + 14*4)) % 60 == 0
step = (days_from_update - (180 + 14*4)) / 60
step = 4 + (days_from_update - (180 + 14*4)) / 60
end

if step == 4
# => To be implemented in model
card.update_attribute(:validity, :disabled)
# p "step: #{step}, days_from_update: #{days_from_update}"

# flag the card and notify the admin if not already done
if step >= 4 && !card.disabled?
card.update_attribute(:validity, :disabled)
Admin::CardMailer.deactivated(card).deliver_now
end

if step != -1
card.update_attribute(:validity, :outdated) if step < 4 && !@card.outdated

# flag the card if not already done
if step >= 0 && step <= 3 && !card.solicited?
card.update_attribute(:validity, :solicited)
end

# notify the card owner
if step >= 0
CardMailer.update_check(card, step).deliver_now
end
end
Expand Down
125 changes: 125 additions & 0 deletions spec/lib/cards_update_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
require 'rails_helper'
require 'rake'

Rails.application.load_tasks

RSpec.describe "cards_update_check.rake" do

cases = [
# days_since_update, before_validity, expected_validity, user_email (step), admin_email
[0, :maintained, "maintained", -1, false],
[1, :maintained, "maintained", -1, false],

[180-1, :maintained, "maintained", -1, false],
[180-1, :solicited, "solicited", -1, false],
[180, :maintained, "solicited", 0, false], # normal case
[180, :solicited, "solicited", 0, false],
[180+1, :solicited, "solicited", -1, false],

[180+14-1, :solicited, "solicited", -1, false],
[180+14, :solicited, "solicited", 1, false], # normal case
[180+14, :maintained, "solicited", 1, false],
[180+14+1, :solicited, "solicited", -1, false],

[180+14*2-1, :solicited, "solicited", -1, false],
[180+14*2, :solicited, "solicited", 2, false], # normal case
[180+14*2+1, :solicited, "solicited", -1, false],

[180+14*3-1, :solicited, "solicited", -1, false],
[180+14*3, :solicited, "solicited", 3, false], # normal case
[180+14*3+1, :solicited, "solicited", -1, false],

[180+14*4-1, :solicited, "solicited", -1, false],
[180+14*4-1, :maintained, "maintained", -1, false],
[180+14*4-1, :disabled, "disabled", -1, false],
[180+14*4, :solicited, "disabled", 4, true], # normal case
[180+14*4, :maintained, "disabled", 4, true],
[180+14*4, :disabled, "disabled", 4, false],
[180+14*4+1, :disabled, "disabled", -1, false],

[180+14*4+60-1, :disabled, "disabled", -1, false],
[180+14*4+60, :disabled, "disabled", 5, false], # normal case
[180+14*4+60, :maintained, "disabled", 5, true],
[180+14*4+60, :solicited, "disabled", 5, true],
[180+14*4+60+1, :disabled, "disabled", -1, false],

[180+14*4+60*2-1, :disabled, "disabled", -1, false],
[180+14*4+60*2, :disabled, "disabled", 6, false], # normal case
[180+14*4+60*2+1, :disabled, "disabled", -1, false],

[180+14*4+60*10-1, :disabled, "disabled", -1, false],
[180+14*4+60*10, :disabled, "disabled", 14, false], # normal case
[180+14*4+60*10+1, :disabled, "disabled", -1, false],
]

cases.each do |t|
describe "#{t}" do
it "should have correct validity" do
b = CardValidity.new(self, t[0], t[1])
b.should_have_validity(t[2])
end

it "should send email to user accordingly" do
b = CardValidity.new(self, t[0], t[1])
if t[3] == -1
b.should_not_send_user_email
else
b.should_send_user_email_with_step(t[3])
end
end

it "should send email to admin accordingly" do
b = CardValidity.new(self, t[0], t[1])
if t[4]
b.should_send_admin_email
else
b.should_not_send_admin_email
end
end
end
end
end

# This is a helper class around a card to check its validity and state after
# calling the "cards_update:check" task.
class CardValidity
def initialize(rspec, days_since_update, before_validity)
@r = rspec

@card = FactoryBot.create(:card, validity: before_validity)
@card.update_attribute(:last_updated, Time.current - days_since_update.days)
end

def should_have_validity(validity)
run_task
@r.expect(@card.validity).to @r.eq(validity)
end

def should_send_user_email_with_step(step)
@r.expect(CardMailer).to @r.receive(:update_check).with(@card, step).and_call_original
run_task
end

def should_not_send_user_email
@r.expect(CardMailer).not_to @r.receive(:update_check).and_call_original
run_task
end

def should_send_admin_email
@r.expect(Admin::CardMailer).to @r.receive(:deactivated).with(@card).and_call_original
run_task
end

def should_not_send_admin_email
@r.expect(Admin::CardMailer).not_to @r.receive(:deactivated).with(@card).and_call_original
run_task
end

private

def run_task
Rake::Task['cards_update:check'].invoke
Rake::Task['cards_update:check'].reenable
@card.reload
end
end
2 changes: 1 addition & 1 deletion spec/mailers/previews/card_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def offline
def update_check
card = FactoryBot.build(:card)
card.id = 1
card.last_updated = Time.now - 6.month
card.last_updated = Time.current - 6.month
CardMailer.update_check(card, 7)
end

Expand Down