def test_authenticate_fails_without_valid_date_payload(user, ss): perimed_jwt = JWT.encode( get_payload(user, {'seconds': -8}), settings['JWT']['SECRET']) header = "Bearer " + perimed_jwt engine = MapistarJWTAuthentication() with pytest.raises(AuthenticationFailed): engine.authenticate(header, settings, ss)
def testautheticate_pass_with_valid_jwt(user, ss): valid_jwt = JWT.encode( get_payload(user, {'seconds': 8}), settings['JWT']['SECRET']) header = "Bearer " + valid_jwt engine = MapistarJWTAuthentication() authed = engine.authenticate(header, settings, ss) assert authed.user == user
def test_user_is_not_active(user, ss): valid_jwt = JWT.encode( get_payload(user, {'seconds': 8}), settings['JWT']['SECRET']) header = "Bearer " + valid_jwt user.is_active = False user.save() engine = MapistarJWTAuthentication() with pytest.raises(Forbidden): engine.authenticate(header, settings, ss)
def test_invalid_user(ss): a = MagicMock() a.id = 35135135135151 valid_jwt = JWT.encode( get_payload(a, {'seconds': 8}), settings['JWT']['SECRET']) header = "Bearer " + valid_jwt engine = MapistarJWTAuthentication() with pytest.raises(BadRequest): engine.authenticate(header, settings, ss)
def client(user): """ Authenticated client """ SECRET = settings['JWT'].get('SECRET') token = JWT.encode(get_payload(user, {'seconds': 60}), secret=SECRET) c = TestClient(app_fix()) c.headers['Authorization'] = "Bearer " + token return c
def test_payload_returned_is_empty(user, ss, monkeypatch): def return_empty_dict(*args): a = MagicMock() a.payload = {} return a monkeypatch.setattr('users.authentication.get_jwt', return_empty_dict) valid_jwt = JWT.encode( get_payload(user, {'seconds': 8}), settings['JWT']['SECRET']) header = "Bearer " + valid_jwt engine = MapistarJWTAuthentication() with pytest.raises(AuthenticationFailed): engine.authenticate(header, settings, ss)
def test_get_payload_user(user): pl = get_payload(user, {'seconds': 3}) assert pl['user_id'] == user.id
def test_get_payload_duration_empty(): with pytest.raises(ValueError): pl = get_payload(user='******', duration={})