#-*- coding: utf-8 -*-
#
# Système de gestion des adhésions de Nos oignons
# Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Then /^aucun email ne doit avoir été envoyé$/ do
  expect(Mail::TestMailer.deliveries.size).to eq(0)
end

Then /^un appel pour la cotisation doit avoir été envoyé$/ do
  expect(Mail::TestMailer.deliveries.size).to eq(1)
  mail = Mail::TestMailer.deliveries.first
  expect(mail.from).to eql([NosOignons::BOARD_EMAIL])
  expect(mail.to).to eql([@last_member['email']])
  expect(mail.subject).to include('Renouvellement de votre cotisation')
  expect(mail.body).to include(@last_member['name'])
  expect(mail.body).to include('30 jours')
end

Then /^l'appel envoyé doit demander de vérifier l'adresse postale$/ do
  mail = Mail::TestMailer.deliveries.first
  expect(mail.body.to_s.gsub(/^ */, '')).to include(@last_member['address'])
end

Then /^un (premier|dernier) rappel pour la cotisation doit avoir été envoyé$/ do |kind|
  mail = Mail::TestMailer.deliveries.first
  expect(mail.subject).to include("(#{kind == 'premier' ? '' : 'dernier '}rappel)")
end

Then /^(\d+) emails? (?:doit|doivent) avoir été envoyés?$/ do |count|
  expect(Mail::TestMailer.deliveries.size).to eq(count.to_i)
end

Then /^des emails doivent avoir été envoyés à Jane, Fatima, Fred et Moly$/ do
  recipients = Mail::TestMailer.deliveries.collect(&:to)
  expect(recipients).to include(['jane@example.org'])
  expect(recipients).to include(['fatima@example.org'])
  expect(recipients).to include(['fred@example.org'])
  expect(recipients).to include(['moly@example.org'])
end

Then /^un email doit avoir été envoyé au comité de déontologie$/ do
  expect(Mail::TestMailer.deliveries.size).to eq(1)
  expect(Mail::TestMailer.deliveries.first.to).to include('deontologie@nos-oignons.net')
end

Then /^il doit contenir les adresses emails de Jane et Fred$/ do
  expect(Mail::TestMailer.deliveries.first.body).to include('jane@example.org')
  expect(Mail::TestMailer.deliveries.first.body).to include('fred@example.org')
end

