test_resources.py 4.2 KB
Newer Older
1
"""Tests for basic requests to all resources as admin."""
2

3
4
5
6
import pytest

from backend.settings import DOMAIN

adietmue's avatar
adietmue committed
7
8
9

def test_create(app):
    """Test creating everything as an admin user."""
10
    with app.admin():
adietmue's avatar
adietmue committed
11
12
13
14
15
16
17
18
19
        lecture = {
            'title': "Awesome Lecture",
            'department': "itet",
            'year': 3,
        }
        lecture_response = app.client.post('lectures',
                                           data=lecture,
                                           assert_status=201)

20
21
22
23
24
25
26
27
        assistant = {
            'name': "Pablo Pablone",
            'email': "pablop@ethz.ch",
        }
        assistant_response = app.client.post('assistants',
                                             data=assistant,
                                             assert_status=201)

adietmue's avatar
adietmue committed
28
29
        course = {
            'lecture': lecture_response['_id'],
30
            'assistant': assistant_response['_id'],
adietmue's avatar
adietmue committed
31
32
33
34
35
36
37
            'room': 'ETZ E 6',
            'spots': 30,
            'signup': {
                'start': '2020-05-01T10:00:00Z',
                'end': '2020-05-05T23:59:59Z',
            },
            'datetimes': [{
adietmue's avatar
adietmue committed
38
39
                'start': '2020-06-05T10:00:00Z',
                'end': '2020-06-05T12:00:00Z',
40
            }, {
adietmue's avatar
adietmue committed
41
42
                'start': '2020-06-06T10:00:00Z',
                'end': '2020-06-06T12:00:00Z',
43
            }],
adietmue's avatar
adietmue committed
44
45
46
47
48
        }
        course_response = app.client.post('courses',
                                          data=course,
                                          assert_status=201)

49
50
        selection = {
            'nethz': 'Pablito',
51
            'course': course_response['_id']
52
53
54
        }
        app.client.post('selections', data=selection, assert_status=201)

adietmue's avatar
adietmue committed
55
56
57
58
        signup = {
            'nethz': "Pablito",
            'course': course_response['_id']
        }
59
60
61
        app.client.post('signups',
                        data=signup,
                        assert_status=201)
62

63
64
        # Payment is tested separately

adietmue's avatar
adietmue committed
65
66
67

def test_no_double_signup(app):
    """Users can signup for several courses, but not for any course twice."""
68
    with app.admin():
adietmue's avatar
adietmue committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        # Create two fake courses to sign up to
        first = str(app.data.driver.db['courses'].insert({}))
        second = str(app.data.driver.db['courses'].insert({}))

        def _signup(course, assert_status):
            signup = {
                'nethz': "Pablito",
                'course': course
            }
            app.client.post('signups',
                            data=signup,
                            assert_status=assert_status)

        # No Double signup to same course
        _signup(first, 201)
        _signup(first, 422)
        # Sign up to other courses still fine
        _signup(second, 201)
87
88
89
90
91
92
93
94


def test_no_patch(app):
    """Test that certain fields cannot be changed.

    These are: Course->lecture and signup->nethz
    """
    no_patch_error = "this field can not be changed with PATCH"
95
96
    with app.admin():
        headers = {'If-Match': 'tag'}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

        # Create fake resources, make sure to set _etag so we can patch
        course = str(app.data.driver.db['courses'].insert({'_etag': 'tag'}))
        signup = str(app.data.driver.db['signups'].insert({'_etag': 'tag'}))
        # Make sure that the objectid of patch data is valid
        new_lecture = str(app.data.driver.db['lectures'].insert({}))

        course_url = '/courses/' + course
        signup_url = '/signups/' + signup

        response = app.client.patch(course_url, headers=headers,
                                    data={'lecture': new_lecture},
                                    assert_status=422)
        assert response["_issues"]["lecture"] == no_patch_error
        response = app.client.patch(signup_url, headers=headers,
                                    data={'nethz': 'lalala'},
                                    assert_status=422)
        assert response["_issues"]["nethz"] == no_patch_error
115
116
117
118
119
120
121
122
123
124
125


@pytest.mark.parametrize('resource', DOMAIN.keys())
def test_no_batch_inserts(app, resource):
    """Test that batch payments are disabled (Eve sends 400 in this case)."""
    with app.admin():
        list_data = [{}, {}]

        app.client.post(resource,
                        data=list_data,
                        assert_status=400)