Пример #1
0
def test_signup_event(client, app):
    # test that signup will be created
    key = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3'
    path = '/api/v1/event/1/users'
    signature = generate_signature(key, path)
    resp = client.post(path,
                       json={"user_id": 3},
                       headers={'signature': signature})
    assert resp.status_code == 201
    assert resp.json['status'] == 'success'

    # test that the signup was inserted into the database
    with app.app_context():
        signup = EventSignup.query.filter_by(user_id=3).filter_by(
            event_id=1).first()
        assert signup

    # test that signup will be created by admin
    admin_key = '4e7fe767-bd2b-4a34-acdf-17985d65830c'
    admin_id = 1
    admin_signature = generate_signature(admin_key, path)
    resp = client.post(path,
                       json={"user_id": 4},
                       headers={
                           'admin_signature': admin_signature,
                           'admin_id': admin_id
                       })
    assert resp.status_code == 201
    assert resp.json['status'] == 'success'

    # test that the signup was inserted into the database
    with app.app_context():
        signup = EventSignup.query.filter_by(user_id=4).filter_by(
            event_id=1).first()
        assert signup
Пример #2
0
def test_signup_event_fail(client, app):
    # test that signature check will fail
    key = '123'
    path = '/api/v1/event/1/users'
    signature = generate_signature(key, path)
    resp = client.post(path,
                       json={"user_id": 3},
                       headers={'signature': signature})
    assert resp.status_code == 401
    assert resp.json['status'] == 'failure'

    # test that user will not be found
    signature = generate_signature(key, path)
    resp = client.post(path,
                       json={"user_id": 5},
                       headers={'signature': signature})
    assert resp.status_code == 404
    assert resp.json['status'] == 'failure'

    # test that event will not be found
    path = '/api/v1/event/3/users'
    key = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3'
    signature = generate_signature(key, path)
    resp = client.post(path,
                       json={"user_id": 3},
                       headers={'signature': signature})
    assert resp.status_code == 404
    assert resp.json['status'] == 'failure'

    # test that signup already exists
    path = '/api/v1/event/1/users'
    signature = generate_signature(key, path)
    resp = client.post(path,
                       json={"user_id": 1},
                       headers={'signature': signature})
    assert resp.status_code == 409
    assert resp.json['status'] == 'failure'

    # test that admin_signature check will fail
    path = '/api/v1/event/1/users'
    admin_key = '123'
    admin_id = 1
    admin_signature = generate_signature(admin_key, path)
    resp = client.post(path,
                       json={"user_id": 4},
                       headers={
                           'admin_signature': admin_signature,
                           'admin_id': admin_id
                       })
    assert resp.status_code == 401
    assert resp.json['status'] == 'failure'
Пример #3
0
    def get(self, event_id):
        resp = {'status': SUCCESS}
        http_code = 200
        try:
            admin_signature = request.headers.get('admin_signature')
            admin_id = request.headers.get('admin_id')
            admin_user = Admin.query.filter_by(user_id=admin_id).first()
            if not admin_user:
                raise UserNotFountError

            expected_signature = generate_signature(admin_user.admin_key, request.path)
            if expected_signature != admin_signature:
                raise SignatureError
            signups = EventSignup.query.filter_by(event_id=event_id).all()
            users = []
            for signup in signups:
                user = User.query.filter_by(id=signup.user_id).first()
                if user:
                    users.append({'id':user.id, 'name': user.name, 'email': user.email})
            resp['data'] = users
        except UserNotFountError:
            http_code = 404
            resp['status'] = FAILURE
            error = {'code': 200404, 'message': 'user not found'}
            resp['data'] = error
        except SignatureError:
            http_code = 401
            resp['status'] = FAILURE
            error = {'code': 200401, 'message': 'signature check failed'}
            resp['data'] = error
        except Exception as err:
            print(f'EventsEndPoint error happened {err}')
            raise err
        resp_str = jsonify(resp)
        return make_response(resp_str, http_code)
Пример #4
0
def test_quit_event_fail(client, app):
    # test that signature check will fail
    key = '123'
    path = '/api/v1/event/1/user/3'
    signature = generate_signature(key, path)
    resp = client.delete(path, headers={'signature': signature})
    assert resp.status_code == 401
    assert resp.json['status'] == 'failure'

    # test that user will not be found
    path = '/api/v1/event/1/user/5'
    signature = generate_signature(key, path)
    resp = client.delete(path, headers={'signature': signature})
    assert resp.status_code == 404
    assert resp.json['status'] == 'failure'

    # test that event will not be found
    path = '/api/v1/event/3/user/3'
    key = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3'
    signature = generate_signature(key, path)
    resp = client.delete(path, headers={'signature': signature})
    assert resp.status_code == 404
    assert resp.json['status'] == 'failure'

    # test that signup does not exists
    path = '/api/v1/event/1/user/4'
    signature = generate_signature(key, path)
    resp = client.delete(path, headers={'signature': signature})
    assert resp.status_code == 404
    assert resp.json['status'] == 'failure'

    # test that admin_signature check will fail
    path = '/api/v1/event/1/user/1'
    admin_key = '123'
    admin_id = 1
    admin_signature = generate_signature(admin_key, path)
    resp = client.delete(path,
                         headers={
                             'admin_signature': admin_signature,
                             'admin_id': admin_id
                         })
    assert resp.status_code == 401
    assert resp.json['status'] == 'failure'
Пример #5
0
 def quit_event(self, event_id, user_id):
     try:
         signature = request.headers.get('signature')
         user = User.query.filter_by(id=user_id).first()
         if not user:
             raise UserNotFountError
         expected_signature = generate_signature(user.password, request.path)
         if expected_signature != signature:
             raise SignatureError
         self.quit_with_id(event_id, user_id)
     except Exception as err:
         print(f'EventsEndPoint signup_event error happened {err}')
         raise err
Пример #6
0
def test_get_signups_of_event(client, app):
    # test that signups of the event will be got by admin
    path = '/api/v1/event/1/users'
    admin_key = '4e7fe767-bd2b-4a34-acdf-17985d65830c'
    admin_id = 1
    admin_signature = generate_signature(admin_key, path)
    resp = client.get(path,
                      headers={
                          'admin_signature': admin_signature,
                          'admin_id': admin_id
                      })
    assert resp.status_code == 200
    assert resp.json['status'] == 'success'
Пример #7
0
    def quit_event_by_admin(self, admin_id, event_id, user_id):
        try:
            admin_signature = request.headers.get('admin_signature')
            admin_user = Admin.query.filter_by(user_id=admin_id).first()
            if not admin_user:
                raise UserNotFountError

            expected_signature = generate_signature(admin_user.admin_key, request.path)
            if expected_signature != admin_signature:
                raise SignatureError
            self.quit_with_id(event_id, user_id)
        except Exception as err:
            print(f'EventsEndPoint signup_event_by_admin error happened {err}')
            raise err
Пример #8
0
    def signup_event_by_admin(self, admin_id, event_id):
        try:
            admin_signature = request.headers.get('admin_signature')
            admin_user = Admin.query.filter_by(user_id=admin_id).first()
            if not admin_user:
                raise UserNotFountError

            expected_signature = generate_signature(admin_user.admin_key, request.path)
            if expected_signature != admin_signature:
                raise SignatureError

            user_id = request.json['user_id']
            if user_id is not None:
                self.signup_with_id(user_id, event_id)
            else:
                raise ValueError("Missing parameter")
        except Exception as err:
            print(f'EventsEndPoint signup_event_by_admin error happened {err}')
            raise err
Пример #9
0
def test_quit_event_failed(client, app):
    assert generate_signature(
        '123', 'abc'
    ) == '8f16771f9f8851b26f4d460fa17de93e2711c7e51337cb8a608a0f81e1c1b6ae'