def test_has_role_unauthenicated_user(self, db): from examples.auth import Auth, Unauthenticated worker_ctx = Mock(context_data={}) dep = Auth.Api(db, worker_ctx) with pytest.raises(Unauthenticated): dep.has_role('admin')
def test_authenticate_bad_password(self, db): from examples.auth import Auth, Unauthenticated worker_ctx = Mock(context_data={}) dep = Auth.Api(db, worker_ctx) with pytest.raises(Unauthenticated): dep.authenticate("matt", "invalid") assert worker_ctx.context_data.get('auth') is None
def test_authenticate_bad_username(self, db): from examples.auth import Auth, Unauthenticated worker_ctx = Mock(context_data={}) dep = Auth.Api(db, worker_ctx) with pytest.raises(Unauthenticated): dep.authenticate("angela", "secret") assert worker_ctx.context_data.get('auth') is None
def test_authenticate(self, db): from examples.auth import Auth, JWT_SECRET worker_ctx = Mock(context_data={}) dep = Auth.Api(db, worker_ctx) token = dep.authenticate("matt", "secret") jwt.decode(token, key=JWT_SECRET, verify=True) assert worker_ctx.context_data['auth'] == token
def test_authenticated_user_does_not_have_role(self, db): from examples.auth import Auth, JWT_SECRET token = jwt.encode( {'username': '******', 'roles': ['dev']}, key=JWT_SECRET ) worker_ctx = Mock(context_data={'auth': token}) dep = Auth.Api(db, worker_ctx) assert not dep.has_role('admin')
def test_check_role(self, db): from examples.auth import Auth, Unauthorized, JWT_SECRET token = jwt.encode( {'username': '******', 'roles': ['dev']}, key=JWT_SECRET ) worker_ctx = Mock(context_data={'auth': token}) dep = Auth.Api(db, worker_ctx) assert dep.check_role('dev') is None with pytest.raises(Unauthorized): dep.check_role('admin')
def test_has_role_invalid_token(self, db): from examples.auth import Auth worker_ctx = Mock(context_data={'auth': 'invalid-token'}) dep = Auth.Api(db, worker_ctx) assert not dep.has_role('admin')