]> nos-oignons.net Git - gestion-adh.git/blob - features/step_definitions/mailman.rb
Switch to Mailman 3 REST API for “ag” subscriptions
[gestion-adh.git] / features / step_definitions / mailman.rb
1 #-*- coding: utf-8 -*-
2 #
3 # Système de gestion des adhésions de Nos oignons
4 # Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 require 'webmock'
20 require 'json'
21 require 'digest'
22
23 Before('@mailman') do
24   @ag_emails = []
25   stub_request(:any, /mailman.example.org/).
26     with(basic_auth: ['mailman_rest_user', 'mailman_rest_pass'])
27   stub_request(:get, 'https://mailman.example.org/3.0/lists/ag@nos-oignons.net/roster/member').
28     with(basic_auth: ['mailman_rest_user', 'mailman_rest_pass']).
29     to_return { |request| {body: roster_member_payload } }
30 end
31
32 def member_id_for_email(email)
33   Digest::SHA1.hexdigest(email)
34 end
35
36 def roster_member_payload
37   # This is an approximation of the actual payload from Mailman, but good enough for our needs
38   payload = {
39     "total_size": @ag_emails.count,
40     "start": 0,
41     "entries": @ag_emails.map { |email| { "email": email, "member_id": member_id_for_email(email) } },
42   }
43   payload.to_json
44 end
45
46 Given /^une liste ag@ avec comme emails inscrits:$/ do |subscriber_list|
47   @ag_emails = subscriber_list.strip.split
48 end
49
50 Then 'la liste ag@ doit avoir reçu l’inscription de {string}' do |email|
51   expect(
52     a_request(:post, "https://mailman.example.org/3.0/members").
53       with(headers: {'Content-Type': 'application/json'},
54            body: hash_including(
55              {"list_id": "ag.nos-oignons.net",
56               "subscriber": email,
57               "pre_verified": true,
58               "pre_confirmed": true,
59               "pre_approved": true,
60              }
61            ),
62           )).to have_been_made
63 end
64
65 Then 'la liste ag@ ne doit pas avoir reçu d’inscription' do
66   expect(
67     a_request(:post, "https://mailman.example.org/3.0/members")
68   ).not_to have_been_made
69 end
70
71 Then 'la liste ag@ doit avoir reçu la désinscription de {string}' do |email|
72   expect(
73     a_request(:delete, "https://mailman.example.org/3.0/members/#{member_id_for_email(email)}")
74   ).to have_been_made
75 end
76