def test_json_renderer(dummy_request: DummyRequest) -> None: """Test that User is correctly rendered for an OpenAPI JSON response.""" dummy_request.create_jwt_token = mock.Mock() dummy_request.create_jwt_token.return_value = "token" user = User(id=1, username="******") renderer = json_renderer() output = renderer(None)(user, {"request": dummy_request}) assert json.loads(output) == { "token": "token", "username": "******", }
def add_users(db: Session) -> None: """Add demo users to db.""" one = User(id=USER_ONE_ID, username="******", password_hash=SECRET) db.add(one) logger.info("User added", username=one.username) two = User(id=USER_TWO_ID, username="******", password_hash=SECRET) db.add(two) logger.info("User added", username=two.username) # # Postman tests expect this user to be present # johnjacob = User( # id=USER_JOHNJACOB_ID, # username="******", # password_hash=SECRET, # ) # db.add(johnjacob) # johnjacob.follows.append(one) # logger.info("User added", username=johnjacob.username) db.flush()
def login(request: Request) -> Response: """User logs in using GitHub.""" response = Response() result = request.authomatic.login(WebObAdapter(request, response), "github") if result is None: return response if result.error: return exception_response(401, json_body={"error": result.error.message}) result.user.update() username = result.user.username user = User.by_username(username, db=request.db) if user is None: return exception_response(401, json_body={"error": "Not authorized."}) return Response(json_body={"token": request.create_jwt_token(str(user.id))})
def add_urls(db: Session) -> None: """Add demo urls to db.""" foo = Url( id=URL_FOO_ID, slug="foo", title="Foö", description="Foö desc", href="https://glicksoftware.com", author=User.by_username("one", db=db), created=datetime(2019, 1, 1, 1, 1, 1), updated=datetime(2019, 2, 2, 2, 2, 2), ) db.add(foo) logger.info("Url added", slug=foo.slug) # bar = Article( # id=ARTICLE_BAR_ID, # slug="bar", # title="Bär", # description="Bär desc", # body="Bär body", # author=User.by_username("one", db=db), # created=datetime(2019, 3, 3, 3, 3, 3), # updated=datetime(2019, 4, 4, 4, 4, 4), # ) # db.add(bar) # logger.info("Article added", slug=bar.slug) # # Postman tests require this user to have at least one article # johnjacob = Article( # id=ARTICLE_JOHNJACOB_ID, # slug="i-am-johnjacob", # title="I am John Jacob", # description="johnjacob desc", # body="johnjacob body", # author=User.by_username("johnjacob", db=db), # created=datetime(2019, 5, 5, 5, 5, 5), # updated=datetime(2019, 6, 6, 6, 6, 6), # ) # db.add(johnjacob) # logger.info("Article added", slug=johnjacob.slug) db.flush()
def test_json_renderer(db: Session, democontent: None) -> None: """Test that Url is correctly rendered for an OpenAPI JSON response.""" user = User.by_username("two", db=db) url = Url.by_slug("foo", db=db) request = DummyRequest() request.user = user renderer = json_renderer() output = renderer(None)(url, {"request": request}) assert json.loads(output) == { "createdAt": "2019-01-01T01:01:01.000Z", "description": "Foö desc", "href": "https://glicksoftware.com", "slug": "foo", "title": "Foö", "updatedAt": "2019-02-02T02:02:02.000Z", }
def test_verify_password(db: Session, democontent: None) -> None: """Test verifying user's password.""" user = User.by_username("one", db) assert user.verify_password("secret") # type: ignore assert not user.verify_password("invalid") # type: ignore
def test_by_shortcuts(db: Session, democontent: None) -> None: """Test that by_* shortcuts work.""" assert User.by_username("one", db) == User.by_id(USER_ONE_ID, db)
def get_user(request: Request) -> t.Optional[User]: """Never to be called directly, exposes request.user.""" return User.by_id(request.authenticated_userid, db=request.db)