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

adietmue's avatar
adietmue committed
3
4
5

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

        course = {
            'lecture': lecture_response['_id'],
19
            'assistant': 'pablo',
adietmue's avatar
adietmue committed
20
21
22
23
24
25
26
            '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
27
28
                'start': '2020-06-05T10:00:00Z',
                'end': '2020-06-05T12:00:00Z',
29
            }, {
adietmue's avatar
adietmue committed
30
31
                'start': '2020-06-06T10:00:00Z',
                'end': '2020-06-06T12:00:00Z',
32
            }],
adietmue's avatar
adietmue committed
33
34
35
36
37
        }
        course_response = app.client.post('courses',
                                          data=course,
                                          assert_status=201)

38
39
        selection = {
            'nethz': 'Pablito',
40
            'course': course_response['_id']
41
42
43
        }
        app.client.post('selections', data=selection, assert_status=201)

adietmue's avatar
adietmue committed
44
45
46
47
        signup = {
            'nethz': "Pablito",
            'course': course_response['_id']
        }
48
49
50
51
52
53
54
        signup_response = app.client.post('signups',
                                          data=signup,
                                          assert_status=201)

        payment = {'signups': [signup_response['_id']]}
        app.client.post('payments', data=payment, assert_status=201)

adietmue's avatar
adietmue committed
55
56
57

def test_no_double_signup(app):
    """Users can signup for several courses, but not for any course twice."""
58
    with app.admin():
adietmue's avatar
adietmue committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        # 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)
77
78
79
80
81
82
83
84


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"
85
86
    with app.admin():
        headers = {'If-Match': 'tag'}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

        # 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