Projet

Général

Profil

Télécharger (5,34 ko) Statistiques
| Branche: | Tag: | Révision:

mandayejs / tests / test_api.py @ b69ac82e

1
import mock
2
import pytest
3

    
4
from django.conf import settings
5

    
6
from mandayejs.mandaye.models import UserCredentials
7
from utils import create_user, create_credentials, get_uuid, get_user
8

    
9

    
10
def test_api_get(client, url):
11
    response = client.get(url)
12
    if client.session.values():
13
        status_code = 200
14
    else:
15
        status_code = 403
16

    
17
    assert response.status_code == status_code
18

    
19
    if status_code == 200:
20
        assert {'login': '', 'password': ''} == response.data
21

    
22

    
23
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
24
def test_signed_api_get(client_service, url_signed):
25
    response = client_service.get(url_signed.url)
26
    if url_signed.orig == 'testserver':
27
        status_code = 200
28
    else:
29
        status_code = 403
30

    
31
    assert response.status_code == status_code
32

    
33
    if status_code == 200:
34
        assert {'login': '', 'password': ''} == response.data
35

    
36

    
37
# POST
38
@mock.patch('mandayejs.mandaye.api.exec_phantom')
39
def test_api_post(mock_phantomjs_result, client, url, payload):
40

    
41
    if client.session.values():
42
        status_code = {'success': 200, 'failure': 401}
43
    else:
44
        status_code = {'success': 403, 'failure': 403}
45

    
46
    if payload.get('name_id_content') == '12345':
47
        response = client.post(url, data=payload, format='json')
48

    
49
        assert response.status_code == status_code['failure']
50

    
51
        if client.session.values():
52
            kevin = get_user(first_name='kevin')
53
            assert kevin.username == payload['name_id_content']
54

    
55
            kevin_uuid = get_uuid(name_id=payload['name_id_content'])
56
            assert kevin_uuid.name_id == '12345'
57
    else:
58
        mock_phantomjs_result.return_value = {"result": "ok"}
59

    
60
        response = client.post(url, data=payload, format='json')
61

    
62
        assert response.status_code == status_code['success']
63

    
64
        if client.session.values():
65
            josh = get_user(username='77777')
66
            josh_creds = UserCredentials.objects.filter(user=josh)[0]
67

    
68
            assert josh_creds.to_login_info()['#login'] == 'josh'
69
            assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
70

    
71

    
72
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
73
@mock.patch('mandayejs.mandaye.api.exec_phantom')
74
def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload):
75
    if url_signed.orig == 'testserver':
76
        status_code = {'success': 200, 'failure': 401}
77
    else:
78
        status_code = {'success': 403, 'failure': 403}
79

    
80
    if payload.get('name_id_content') == '12345':
81
        response = client_service.post(url_signed.url, data=payload, format='json')
82

    
83
        assert response.status_code == status_code['failure']
84

    
85
        if url_signed.orig == 'testserver':
86
            kevin = get_user(first_name='kevin')
87
            assert kevin.username == payload['name_id_content']
88

    
89
            kevin_uuid = get_uuid(name_id=payload['name_id_content'])
90
            assert kevin_uuid.name_id == '12345'
91
    else:
92
        mock_phantomjs_result.return_value = {"result": "ok"}
93

    
94
        response = client_service.post(url_signed.url, data=payload, format='json')
95

    
96
        assert response.status_code == status_code['success']
97

    
98
        if url_signed.orig == 'testserver':
99
            josh = get_user(username='77777')
100
            josh_creds = UserCredentials.objects.filter(user=josh)[0]
101

    
102
            assert josh_creds.to_login_info()['#login'] == 'josh'
103
            assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
104

    
105

    
106
# DELETE
107
def test_api_delete(client, url):
108
    if client.session.values():
109
        status_code = {'success': 200, 'failure': 404}
110
    else:
111
        status_code = {'success': 403, 'failure': 403}
112

    
113
    kevin = get_user(first_name='kevin')
114
    assert UserCredentials.objects.filter(user=kevin).exists() is False
115
    response = client.delete(url, data={'name_id_content': '12345'}, format='json')
116
    assert response.status_code == status_code['failure']
117

    
118
    josh = create_user(username='77777')
119
    create_credentials(josh, {
120
        'login': 'josh',
121
        'password': 'josh password'})
122

    
123
    assert UserCredentials.objects.filter(user=josh).exists() is True
124
    response = client.delete(url, data={'name_id_content': '77777'}, format='json')
125
    assert response.status_code == status_code['success']
126
    if client.session.values():
127
        assert UserCredentials.objects.filter(user=josh).exists() is False
128

    
129

    
130
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
131
def test_signed_api_delete(client_service, url_signed):
132
    if url_signed.orig == 'testserver':
133
        status_code = {'success': 200, 'failure': 404}
134
    else:
135
        status_code = {'success': 403, 'failure': 403}
136

    
137
    kevin = get_user(first_name='kevin')
138
    assert UserCredentials.objects.filter(user=kevin).exists() is False
139
    response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json')
140
    assert response.status_code == status_code['failure']
141

    
142
    josh = create_user(username='77777')
143
    create_credentials(josh, {
144
        'login': 'josh',
145
        'password': 'josh password'
146
    })
147

    
148
    assert UserCredentials.objects.filter(user=josh).exists() is True
149
    response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json')
150
    assert response.status_code == status_code['success']
151
    if url_signed.orig == 'testserver':
152
        assert UserCredentials.objects.filter(user=josh).exists() is False
(5-5/8)