Projet

Général

Profil

Télécharger (3,53 ko) Statistiques
| Branche: | Tag: | Révision:

root / tests / test_hobo_notify.py @ e5b05b4d

1
import pytest
2
from copy import deepcopy
3
import json
4

    
5
from corbo.models import Subscription
6
from corbo.hobo_agent.management.commands.hobo_notify import Command
7
from django.core.management import call_command
8
from django.db.models import Q
9

    
10
NOTIFICATION = {'@type': 'provision',
11
                'objects': {'@type': 'user',
12
                            'data': [
13
                                {'uuid': 'uuid1', 'email': 'foo1@example.net', 'mobile': '0504030201'},
14
                                {'uuid': 'uuid2', 'email': 'bar1@example.net', 'mobile': '0009080706'},
15
                            ]},
16
                'audience': [],
17
                'full': False
18
}
19

    
20

    
21
def test_notify_provision(tenant, subscriptions):
22
    command = Command()
23
    command.process_notification(tenant, NOTIFICATION)
24
    assert Subscription.objects.count() == len(subscriptions)
25
    for user in NOTIFICATION['objects']['data']:
26
        assert Subscription.objects.filter(uuid=user['uuid'], identifier='mailto:%(email)s' % user).exists()
27
        assert Subscription.objects.filter(uuid=user['uuid'], identifier='sms:%(mobile)s' % user).exists()
28

    
29

    
30
def test_notify_other_provision(tenant, subscriptions):
31
    role_notification = deepcopy(NOTIFICATION)
32
    role_notification['objects']['@type'] = 'role'
33
    command = Command()
34
    command.process_notification(tenant, role_notification)
35
    assert Subscription.objects.count() == len(subscriptions)
36
    for subscription in subscriptions:
37
        assert Subscription.objects.filter(uuid=subscription.uuid, identifier=subscription.identifier)
38

    
39

    
40
def test_notify_deprovision(tenant, subscriptions):
41
    deprovision_notification = deepcopy(NOTIFICATION)
42
    deprovision_notification['@type'] = 'deprovision'
43
    command = Command()
44
    command.process_notification(tenant, deprovision_notification)
45
    for user in deprovision_notification['objects']['data']:
46
        assert not Subscription.objects.filter(uuid=user['uuid']).exists()
47

    
48

    
49
def test_notify_full(tenant, subscriptions):
50
    full_notification = deepcopy(NOTIFICATION)
51
    full_notification['full'] = True
52
    command = Command()
53
    command.process_notification(tenant, full_notification)
54
    # strings empty and null are the same for CharFields
55
    assert Subscription.objects.filter(Q(uuid='')|Q(uuid__isnull=True)).count() == 8
56
    assert Subscription.objects.exclude(Q(uuid='')|Q(uuid__isnull=True)).count() == 8
57
    full_notification['objects']['data'] = []
58
    command.process_notification(tenant, full_notification)
59
    # check all remaining subscriptions have empty uuids
60
    for subscription in Subscription.objects.all():
61
        assert not subscription.uuid
62

    
63

    
64
def test_notify_emails_only(tenant, subscriptions):
65
    email_notification = deepcopy(NOTIFICATION)
66
    for user in email_notification['objects']['data']:
67
        user['mobile'] = None
68
        user['email'] = 'new-%(email)s' % user
69
    command = Command()
70
    command.process_notification(tenant, email_notification)
71
    for user in email_notification['objects']['data']:
72
        assert not Subscription.objects.filter(uuid=user['uuid'], identifier__startswith='sms:').exists()
73

    
74

    
75
def test_notify_mobiles_only(tenant, subscriptions):
76
    mobile_notification = deepcopy(NOTIFICATION)
77
    for user in mobile_notification['objects']['data']:
78
        user['email'] = None
79
        user['mobile'] = '0%(mobile)s' % user
80
    command = Command()
81
    command.process_notification(tenant, mobile_notification)
82
    for user in mobile_notification['objects']['data']:
83
        assert not Subscription.objects.filter(uuid=user['uuid'], identifier__startswith='mailto:').exists()
(6-6/9)