Exemple #1
0
    def test_req_date_query(self):

        school1 = add_school(name='testschool')
        school2 = add_school(name='testschool2')

        teacher1 = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                            TEACHER, 'MAO', school1.id)

        teacher2 = add_user('oliver keers', '*****@*****.**', 'keerspass',
                            TEACHER, 'KEO', school2.id)
        teacher3 = add_user('bobby', '*****@*****.**', 'bobpass', TEACHER, 'CUB',
                            school2.id)

        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school2.id)

        # other_req
        add_req("teacher 1 school 1 current", "equipment", "notes",
                datetime(2018, 5, 27, 23, 59), teacher1.id, school1.id)

        # other_req2
        add_req("SHOULD RETURN", "equipment3", "notes3",
                datetime(2018, 5, 21, 9, 0, 0), teacher2.id, school2.id)

        add_req("SHOULD RETURN", "equipment3", "notes3",
                datetime(2018, 5, 21, 9, 0, 0), teacher2.id, school2.id)

        add_req("SHOULD RETURN", "equipment5", "notes5",
                datetime(2018, 5, 25, 15, 10), teacher3.id, school2.id)

        add_req("OLD REQ", "equipment6", "notes6",
                datetime(2018, 5, 18, 9, 0, 0), teacher3.id, school2.id)

        wb = datetime(2018, 5, 21).strftime(DATE_FORMAT)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.get(
                '/reqs?wb=' + wb, headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())

            self.assertEqual(response.status_code, 200)

            for req in data['data']:
                self.assertEqual(req['title'], "SHOULD RETURN")

            self.assertIn('success', data['status'])
Exemple #2
0
    def test_get_schools_reqs_as_technician(self):
        """ensure get /reqs as technician returns that schools reqs only"""

        school1 = add_school(name='testschool1')
        school2 = add_school(name='testschool2')

        teacher1 = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                            TEACHER, 'MAO', school1.id)
        teacher2 = add_user('oliver keers', '*****@*****.**', 'keerspass',
                            TEACHER, 'KEO', school2.id)

        tech = add_user('Gary', '*****@*****.**', 'garypass', TECHNICIAN, 'GAY',
                        school1.id)

        now = datetime.now() + timedelta(days=5)

        from_str = datetime.now().strftime(DATE_FORMAT)
        to_str = (datetime.now() + timedelta(days=10)).strftime(DATE_FORMAT)

        add_req("title1", "equipment", "notes", now, teacher1.id, school1.id)

        add_req("title2", "equipment2", "notes2", now, teacher1.id, school1.id)

        add_req("title3", "equipment3", "notes3", now, teacher2.id, school2.id)
        add_req("title4", "equipment4", "notes4", now, teacher2.id, school2.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.get(
                '/reqs?wb=' + from_str,
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())

            self.assertEqual(response.status_code, 200)
            self.assertEqual(len(data['data']), 2)

            for req in data['data']:
                self.assertEqual(req['school_id'], tech.school_id)

            self.assertIn('success', data['status'])
Exemple #3
0
    def test_mark_req_as_done_fails_malformed_patch(self):
        """ensure technician can mark req as done"""

        school = add_school(name='testschool')
        teacher = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                           TEACHER, 'MAO', school.id)

        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher.id,
                      school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/reqs/' + str(req.id),
                content_type='application/json',
                data=json.dumps([{}]),
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('Malformed patch.'.format(req.id), data['message'])
            self.assertIn('fail', data['status'])
Exemple #4
0
    def test_add_req(self):

        now = datetime.now()
        school = add_school('testschool')
        user = add_user(
            'ollie',
            '*****@*****.**',
            'testpass',
            TEACHER,
            'MAO',
            school.id)

        req = add_req(
            'testtitle',
            'testequip',
            'testnotes',
            now,
            user.id,
            school.id)
        self.assertTrue(req.id)
        self.assertEqual(req.title, 'testtitle')
        self.assertEqual(req.equipment, 'testequip')
        self.assertEqual(req.notes, 'testnotes')
        self.assertEqual(req.time, now)
        self.assertFalse(req.isDone)
        self.assertFalse(req.hasIssue)
        self.assertEqual(req.user_id, user.id)
        self.assertEqual(req.school_id, school.id)
Exemple #5
0
    def test_add_req_invalid_json(self):
        """ensure post req with no school_id raises exception"""

        school = add_school('testschool')
        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.post(
                '/reqs',
                data=json.dumps({}),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('Invalid payload.', data['message'])
            self.assertIn('fail', data['status'])
Exemple #6
0
    def test_delete_req_doesnt_exist(self):
        """ensure correct response if non-existant req is deleted."""

        school = add_school(name='testschool')
        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.delete(
                '/reqs/100', headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 404)
            self.assertIn('Req does not exist.', data['message'])
            self.assertIn('fail', data['status'])
Exemple #7
0
    def test_email_check(self):

        school = add_school('holyfamily')
        add_user(name='Oliver Mansell',
                 staff_code='MAO',
                 email='*****@*****.**',
                 password='******',
                 school_id=school.id,
                 admin=True,
                 role_code=TEACHER)

        with self.client:
            response = self.client.get('/[email protected]',
                                       content_type='application/json')

            data = json.loads(response.data.decode())

            self.assertTrue(data['available'])

        with self.client:
            response = self.client.get(
                '/[email protected]',
                content_type='application/json')

            data = json.loads(response.data.decode())

            self.assertFalse(data['available'])
Exemple #8
0
    def test_encode_auth_token(self):
        school = add_school('testschool')

        user = add_user('justatest', '*****@*****.**', 'test', TEACHER, 'MAO',
                        school.id)
        auth_token = user.encode_auth_token(user.id)
        self.assertTrue(isinstance(auth_token, bytes))
Exemple #9
0
    def test_add_req_no_title(self):
        """ensure post req with no title raises exception"""

        school = add_school('testschool')
        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.post(
                '/reqs',
                data=json.dumps({
                    'equipment': 'equipmenttest',
                    'notes': 'notestest',
                    'lesson_id': 1,
                    'currentWbStamp': '20180416'
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())

            self.assertEqual(response.status_code, 400)
            self.assertIn('You must provide a title for your requisition.',
                          data['message'])
            self.assertIn('fail', data['status'])
Exemple #10
0
    def test_staff_populate_from_SIMS_with_extant_users(self):

            school = add_school('Holy Family Catholic School')

            add_user(
                name='ollie mansell',
                email='*****@*****.**',
                password='******',
                role_code=TEACHER,
                admin=True,
                staff_code='MAO',
                school_id=school.id
            )

            add_user(
                name='denise baxter',
                email='*****@*****.**',
                password='******',
                role_code=TEACHER,
                staff_code='BAD',
                school_id=school.id
            )

            with self.client:
                resp_login = self.client.post(
                    '/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json'
                )
                token = json.loads(resp_login.data.decode())['user']['token']

                response = self.client.post(
                    '/schools/' + str(school.id) + '/staff',
                    content_type='application/json',
                    data=json.dumps(
                        {"filename": "project/api/staffinfo.xlsx"}
                        ),
                    headers={'Authorization': f'Bearer {token}'})

                data = json.loads(response.data.decode())

                self.assertIn(
                    "Please ensure these users are correct.",
                    data['message'])
                self.assertEqual(response.status_code, 200)
                self.assertEqual(
                    len(data['warning']['skipped_users']), 2)
                self.assertIn(
                    '*****@*****.**',
                    data['warning']['skipped_users'])
                self.assertIn(
                    '*****@*****.**',
                    data['warning']['skipped_users'])

                self.assertEqual(response.status_code, 200)
                self.assertIn("success", data['status'])
Exemple #11
0
    def test_passwords_are_random(self):
        school = add_school('testschool')

        user_one = add_user('justatest', '*****@*****.**', 'test', TEACHER,
                            'MAO', school.id)
        user_two = add_user('justatest', '*****@*****.**', 'test', TEACHER,
                            'MAO', school.id)
        self.assertNotEqual(user_one.password, user_two.password)
Exemple #12
0
    def test_req_edit_by_teacher_not_owned_by_teacher(self):
        """ensure correct response if req is edited by a
         teacher who doesn't own it"""

        school = add_school(name='testschool')

        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)
        teacher2 = add_user('oliver keers', '*****@*****.**', 'keerspass',
                            TEACHER, 'KEO', school.id)

        req = add_req("title", "equipment", "notes", datetime.now(),
                      teacher2.id, school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/reqs/' + str(req.id),
                content_type='application/json',
                data=json.dumps([{
                    "op": "replace",
                    "path": "/title",
                    "value": "new title"
                }, {
                    "op": "replace",
                    "path": "/equipment",
                    "value": "new equipment"
                }, {
                    "op": "replace",
                    "path": "/notes",
                    "value": "new notes"
                }]),
                headers={'Authorization': f'Bearer {token}'})

            req_from_db = Req.query.get(req.id)

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 401)
            # ensure req is unchanged
            self.assertEqual(req_from_db.title, "title")
            self.assertEqual(req_from_db.equipment, "equipment")
            self.assertEqual(req_from_db.notes, "notes")
            self.assertIn(
                'You are not authorised to do that.'.format(req_from_db.id),
                data['message'])
            self.assertIn('fail', data['status'])
Exemple #13
0
    def test_req_edit_by_teacher_after_marked_done(self):
        """ensure correct response if req is edited by a
        teacher after marked as done"""

        school = add_school(name='testschool')
        teacher = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                           TEACHER, 'MAO', school.id)
        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher.id,
                      school.id)
        req.isDone = True

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/reqs/' + str(req.id),
                content_type='application/json',
                data=json.dumps([{
                    "op": "replace",
                    "path": "/title",
                    "value": "new title"
                }, {
                    "op": "replace",
                    "path": "/equipment",
                    "value": "new equipment"
                }, {
                    "op": "replace",
                    "path": "/notes",
                    "value": "new notes"
                }]),
                headers={'Authorization': f'Bearer {token}'})

            req_from_db = Req.query.get(req.id)

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 401)
            self.assertEqual(req_from_db.title, "title")
            self.assertEqual(req_from_db.equipment, "equipment")
            self.assertEqual(req_from_db.notes, "notes")
            self.assertIn(
                'Req {} has been marked as done and cannot be edited.'.format(
                    req_from_db.id), data['message'])
            self.assertIn('fail', data['status'])
Exemple #14
0
    def test_add_user(self):
        school = add_school('testschool')

        user = add_user('justatest', '*****@*****.**', 'test', TEACHER, 'MAO',
                        school.id)
        self.assertTrue(user.id)
        self.assertEqual(user.name, 'justatest')
        self.assertEqual(user.email, '*****@*****.**')
        self.assertTrue(user.password)
        self.assertEqual(user.role_code, TEACHER)
        self.assertEqual(user.school_id, 1)
Exemple #15
0
    def test_delete_req_wrong_school(self):
        """ensure correct response if req is
            deleted by staff from wrong school"""

        school = add_school(name='testschool')
        school2 = add_school(name='testschool2')
        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)
        teacher2 = add_user('ollie keers', '*****@*****.**', 'keopass', TEACHER,
                            'MAO', school2.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher2.id,
                      school2.id)

        req_id_retain = req.id

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.delete(
                '/reqs/' + str(req.id),
                headers={'Authorization': f'Bearer {token}'})

            req = Req.query.get(req_id_retain)
            self.assertTrue(req is not None)

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 401)
            self.assertIn('You are not authorised to do that.',
                          data['message'])
            self.assertIn('fail', data['status'])
Exemple #16
0
    def test_time_survives_trip_to_dict_and_back(self):
        school1 = add_school(name='testschool')
        teacher1 = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                            TEACHER, 'MAO', school1.id)
        my_req = add_req("title1", "equipment1", "notes1", datetime.now(),
                         teacher1.id, school1.id)

        self.assertTrue(isinstance(my_req.time, datetime))

        my_req_d = my_req.asdict(exclude_pk=True)

        my_req_d['title'] = 'new title'

        my_req.fromdict(my_req_d)

        self.assertTrue(isinstance(my_req.time, datetime))
Exemple #17
0
    def test_registered_user_login(self):
        school = add_school('testschool')

        with self.client:
            add_user('name', '*****@*****.**', 'test', 1, 'MAO', school.id)
            response = self.client.post('/auth/login',
                                        data=json.dumps({
                                            'email': '*****@*****.**',
                                            'password': '******'
                                        }),
                                        content_type='application/json')
            data = json.loads(response.data.decode())
            self.assertTrue(data['status'] == 'success')
            self.assertTrue(data['message'] == 'Successfully logged in.')
            self.assertTrue(data['user']['token'])
            self.assertTrue(response.content_type == 'application/json')
            self.assertEqual(response.status_code, 200)
Exemple #18
0
    def test_add_user_duplicate_email(self):
        school = add_school('testschool')

        add_user('justatest', '*****@*****.**', 'test', TEACHER, 'MAO',
                 school.id)

        dupe_user = {
            'name': 'justatest',
            'email': '*****@*****.**',
            'password': '******',
            'role_code': TEACHER,
            'staff_code': 'MAO',
            'school_id': school.id
        }

        duplicate_user = User(user_info=dupe_user)
        db.session.add(duplicate_user)
        self.assertRaises(IntegrityError, db.session.commit)
Exemple #19
0
    def test_staff_lesson_room_populate_from_SIMS_missing_staff_code(self):

            school = add_school('Holy Family Catholic School')

            add_user(
                name='ollie mansell',
                email='*****@*****.**',
                password='******',
                role_code=TEACHER,
                admin=True,
                staff_code='MAO',
                school_id=school.id
            )
            with self.client:
                resp_login = self.client.post(
                    '/auth/login',
                    data=json.dumps({
                        'email': '*****@*****.**',
                        'password': '******'
                    }),
                    content_type='application/json'
                )
                token = json.loads(resp_login.data.decode())['user']['token']

                response = self.client.post(
                    '/schools/' + str(school.id) + '/staff',
                    content_type='application/json',
                    data=json.dumps(
                        {"filename":
                            "project/api/staffinfo_missing_staff_code.xlsx"}
                        ),
                    headers={'Authorization': f'Bearer {token}'})

                data = json.loads(response.data.decode())

                self.assertIn(
                    "fail",
                    data['status'])
                self.assertIn(
                    'Staff codes are missing from the uploaded file.',
                    data['message']
                )

                self.assertEqual(response.status_code, 401)
Exemple #20
0
    def test_user_registration_invalid_json_keys_no_email(self):
        school = add_school('testschool')

        with self.client:
            response = self.client.post(
                '/auth/register',
                data=json.dumps({
                    'name': 'justatest',
                    'password': '******',
                    'role_code': TEACHER,
                    'staff_code': "MAO",
                    'school_id': school.id
                }),
                content_type='application/json',
            )
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('No email provided.', data['message'])
            self.assertIn('fail', data['status'])
Exemple #21
0
    def test_add_req_has_correct_user_and_school_id(self):
        """Ensure a new req gets correct user_id from auth token."""

        school = add_school('testschool')

        user = add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER,
                        'MAO', school.id)

        db.session.commit()

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']
            resp = User.decode_auth_token(token)

            response = self.client.post(
                '/reqs',
                data=json.dumps({
                    'title': 'reqtitletest',
                    'equipment': 'equipmenttest',
                    'notes': 'notestest',
                    'lesson_id': 1,
                    'currentWbStamp': '20180416'
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})

            new_req = Req.query.filter_by(title='reqtitletest').first()
            user = User.query.filter_by(id=int(resp)).first()

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 201)
            self.assertIn('reqtitletest was added!', data['message'])
            self.assertEqual(new_req.user_id, user.id)
            self.assertEqual(new_req.school_id, user.school_id)
            self.assertIn('success', data['status'])
Exemple #22
0
    def test_req_marked_hasIssue_by_teacher(self):
        """ensure correct response if req
        is marked as having issue by a teacher"""

        school = add_school(name='testschool')
        teacher = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                           TEACHER, 'MAO', school.id)

        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher.id,
                      school.id)
        req.hasIssue = False

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/reqs/' + str(req.id),
                content_type='application/json',
                data=json.dumps([{
                    "op": "replace",
                    "path": "/hasIssue",
                    "value": True
                }]),
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 401)
            self.assertIn('You are not authorised to do that.',
                          data['message'])
            self.assertIn('fail', data['status'])
Exemple #23
0
    def test_mark_req_as_done(self):
        """ensure technician can mark req as done"""

        school = add_school(name='testschool')
        teacher = add_user('ollie mansell', '*****@*****.**', 'olliepass',
                           TEACHER, 'MAO', school.id)
        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher.id,
                      school.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/reqs/' + str(req.id),
                content_type='application/json',
                data=json.dumps([{
                    "op": "replace",
                    "path": "/isDone",
                    "value": True
                }]),
                headers={'Authorization': f'Bearer {token}'})

            req_from_db = Req.query.get(req.id)

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertIn('Req {} has been updated.'.format(req.id),
                          data['message'])
            self.assertEqual(req_from_db.isDone, True)
            self.assertIn('success', data['status'])
Exemple #24
0
    def test_delete_req_by_technician(self):
        """ensure correct response if req is deleted by a technician"""

        school = add_school(name='testschool')

        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)
        teacher2 = add_user('ollie keers', '*****@*****.**', 'keopass', TEACHER,
                            'MAO', school.id)

        add_user('gary tyler', '*****@*****.**', 'garypass', TECHNICIAN, 'TGA',
                 school.id)

        now = datetime.now()
        req = add_req("title", "equipment", "notes", now, teacher2.id,
                      school.id)

        req_id_retain = req.id

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.delete(
                '/reqs/' + str(req.id),
                headers={'Authorization': f'Bearer {token}'})

            req = Req.query.get(req_id_retain)
            self.assertTrue(req is None)

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 200)
            self.assertIn('Req {} has been deleted.'.format(req_id_retain),
                          data['message'])
            self.assertIn('success', data['status'])
Exemple #25
0
    def test_user_registration_duplicate_email(self):
        school = add_school('testschool')
        add_user('name', '*****@*****.**', 'test', 1, 'MAO', school.id)

        with self.client:
            response = self.client.post(
                '/auth/register',
                data=json.dumps({
                    'name': 'michael',
                    'email': '*****@*****.**',
                    'password': '******',
                    'role_code': TEACHER,
                    'staff_code': "MAO",
                    'school_id': 1
                }),
                content_type='application/json',
            )
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('That email is already taken.', data['message'])
            self.assertIn('fail', data['status'])
Exemple #26
0
    def test_patch_preferences_invalid_JSON(self):
        school = add_school('Holy Family Catholic School')

        user = add_user(
            name='ollie mansell',
            email='*****@*****.**',
            password='******',
            role_code=TEACHER,
            admin=True,
            staff_code='MAO',
            school_id=school.id
        )
        db.session.add(user)
        db.session.commit()

        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps({
                    'email': '*****@*****.**',
                    'password': '******'
                }),
                content_type='application/json'
            )
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/schools/preferences',
                content_type='application/json',
                data=json.dumps([{}]),
                headers={'Authorization': f'Bearer {token}'}
            )

            data = json.loads(response.data.decode())

            self.assertIn(
                'Malformed patch.',
                data['message'])
            self.assertEqual(response.status_code, 400)
            self.assertIn("fail", data['status'])
Exemple #27
0
    def test_add_req_technician(self):
        """ ensure can't post req as technician"""

        school = add_school('testschool')

        add_user('ollie mansell', '*****@*****.**', 'olliepass', TECHNICIAN,
                 'MAO', school.id)

        db.session.commit()

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.post(
                '/reqs',
                data=json.dumps({
                    'title': 'reqtitletest',
                    'equipment': 'equipmenttest',
                    'notes': 'notestest',
                    'lesson_id': 1,
                    'currentWbStamp': '20180416'
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('As a technician you cannot submit requisitions.',
                          data['message'])
            self.assertIn('fail', data['status'])
Exemple #28
0
    def test_add_req(self):
        """Ensure a new req can be added to the database."""
        school = add_school('testschool')

        add_user('ollie mansell', '*****@*****.**', 'olliepass', TEACHER, 'MAO',
                 school.id)

        db.session.commit()

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.post(
                '/reqs',
                data=json.dumps({
                    'title': 'reqtitletest',
                    'equipment': 'equipmenttest',
                    'notes': 'notestest',
                    'lesson_id': 1,
                    'currentWbStamp': '20180416'
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 201)
            self.assertIn('reqtitletest was added!', data['message'])
            self.assertIn('success', data['status'])
Exemple #29
0
    def test_patch_school_with_preferences_update(self):

        school = add_school('Holy Family Catholic School')

        user = add_user(
            name='ollie mansell',
            email='*****@*****.**',
            password='******',
            role_code=TEACHER,
            staff_code='MAO',
            admin=True,
            school_id=school.id
        )
        db.session.add(user)
        db.session.commit()

        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps({
                    'email': '*****@*****.**',
                    'password': '******'
                }),
                content_type='application/json'
            )
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/schools/preferences',
                content_type='application/json',
                data=json.dumps([{
                    "op": "replace",
                    "path": "/days_notice", "value": 3
                    },
                    {
                    "op": "replace",
                    "path": "/reminder_emails", "value": True
                    },
                    {
                    "op": "replace",
                    "path": "/weeks_timetable", "value": 2
                    },
                    {
                    "op": "replace",
                    "path": "/term_dates", "value":
                    [
                        HalfTerm("20170901", "20171020"),
                        HalfTerm("20171030", "20171220"),
                        HalfTerm("20180103", "20180209"),
                        HalfTerm("20180219", "20180329"),
                        HalfTerm("20180416", "20180525"),
                        HalfTerm("20180604", "20180720")
                    ],
                    },
                    {
                        "op": "replace",
                        "path": "/period_start_times", "value":
                        {'1': '0900',
                            '2': '1000',
                            '3': '1120',
                            '4': '1220',
                            '5': '1410',
                            '6': '1510'}
                    }
                    ]),
                headers={'Authorization': f'Bearer {token}'}
            )

            data = json.loads(response.data.decode())

            self.assertIn(
                "Preferences for {} have been updated.".format(school.name),
                data['message'])
            self.assertEqual(response.status_code, 200)
            self.assertEqual(school.preferences['reminder_emails'], True)
            self.assertEqual(school.preferences['days_notice'], 3)
            self.assertEqual(school.preferences['weeks_timetable'], 2)
            self.assertEqual(
                school.preferences['term_dates'][0][0],
                "20170901")
            self.assertEqual(
                school.preferences['term_dates'][5][1], "20180720")

            self.assertIn("success", data['status'])
            self.assertIn(
                "Holy Family Catholic School", data['data']['school']['name']
            )
Exemple #30
0
    def test_update_period_times_invalid_period_length(self):

        school = add_school('Holy Family Catholic School')

        add_user(
            name='ollie mansell',
            email='*****@*****.**',
            password='******',
            role_code=TEACHER,
            admin=True,
            staff_code='MAO',
            school_id=school.id
        )

        with self.client:
            resp_login = self.client.post(
                '/auth/login',
                data=json.dumps({
                    'email': '*****@*****.**',
                    'password': '******'
                }),
                content_type='application/json'
            )
            token = json.loads(resp_login.data.decode())['user']['token']

            response = self.client.patch(
                '/schools/preferences',
                content_type='application/json',
                data=json.dumps(
                    [
                        {
                            "op": "replace",
                            "path": "/period_start_times", "value":
                            {'1': '0900',
                                '2': '1000',
                                '3': '1120',
                                '4': '1220',
                                '5': '1410',
                                '6': '1510'}
                        },
                        {
                            "op": "replace",
                            "path": "/period_length_in_minutes",
                            "value": "01:00"
                        }
                    ]),
                headers={'Authorization': f'Bearer {token}'})

            data = json.loads(response.data.decode())

            self.assertIn(
                "invalid literal",
                data['message'])
            self.assertEqual(response.status_code, 400)
            self.assertEqual(school.preferences['reminder_emails'], False)
            self.assertEqual(school.preferences['days_notice'], 7)
            self.assertEqual(school.preferences['weeks_timetable'], 1)
            self.assertEqual(
                school.preferences['period_start_times'], {})
            self.assertEqual(
                school.preferences['period_length_in_minutes'], 60)
            # self.assertEqual(
            #     school.preferences['term_dates']['autm_h1_s'],
            #     start_of_term.strftime(DATE_FORMAT))

            self.assertIn("fail", data['status'])