def test_token_auth(SessionLocal): db = SessionLocal() user_data = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } user_scheme = UserCreate(**user_data) create_user_query(db, user_scheme) res = client.post( '/auth/token', data=( # data b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" b'Content-Disposition: form-data; name="username"\r\n\r\n' b"[email protected]\r\n" # data b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" b'Content-Disposition: form-data; name="password"\r\n\r\n' b"test1234\r\n" b"--a7f7ac8d4e2e437c877bb7b8d7cc549c--\r\n"), headers={ "Content-Type": "multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c" }, ) assert res.status_code == 200 res = client.post( '/auth/token', data=( # data b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" b'Content-Disposition: form-data; name="username"\r\n\r\n' b"[email protected]\r\n" # data b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" b'Content-Disposition: form-data; name="password"\r\n\r\n' b"incorrect\r\n" b"--a7f7ac8d4e2e437c877bb7b8d7cc549c--\r\n"), headers={ "Content-Type": "multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c" }, ) assert res.status_code == 401
def test_delete_post(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) category = crud.create_category(db, schemas.CreateCategory(**{"name": 'cat'})) series = crud.create_series(db, schemas.CreateSeries(**{"name": 'ser'})) tag = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag'})) tag_name = tag.name posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'category_id': category.id, 'series_id': series.id, 'tag_ids': [tag.id], } post = crud.create_post(db, schemas.CreatePost(**posts)) db.close() db = SessionLocal() crud.delete_post(db, post.id) db.close() db = SessionLocal() post = crud.get_post(db, post.id) assert post is None db.close() db = SessionLocal() _tag = crud.get_tag(db, tag_name) assert _tag.name == tag_name, 'tag is deleted' db.close()
def test_filter_by_tags(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) tag1 = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag1'})) tag2 = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag2'})) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'tag_ids': [tag1.id], } crud.create_post(db, schemas.CreatePost(**posts)) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'tag_ids': [tag1.id, tag2.id], } crud.create_post(db, schemas.CreatePost(**posts)) q = db.query(models.Post) q = crud.filter_post_by_tags(q, [tag1.id, tag2.id]) assert len(q.all()) == 1 db.close()
def create_user( user: schemas.UserCreate, db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user), ): db_user = get_user_by_email_query(db, email=user.email) if db_user: raise HTTPException(status_code=400, detail="Email already registered") return create_user_query(db=db, user=user)
def test_get_authenticated_user(SessionLocal): db = SessionLocal() user_data = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } user_scheme = UserCreate(**user_data) create_user_query(db, user_scheme) # success user = get_authenticated_user(db, user_data['email'], user_data['password']) assert user.username == user_data['username'] # user does not exist user = get_authenticated_user(db, 'no', user_data['password']) assert user is None # invalid password user = get_authenticated_user(db, user_data['email'], 'no') assert user is None
async def test_get_current_user(SessionLocal): user_data = { 'username': '******', 'email': '*****@*****.**', 'password': '******' } db = SessionLocal() user_scheme = UserCreate(**user_data) created_user_res = create_user_query(db, user_scheme) assert created_user_res token_data = schemas.TokenData(username=user_data['email']) current_user = await get_current_user(db=db, token_data=token_data) assert current_user.email == user_data['email']
def test_filter_by_is_public(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'is_public': False } crud.create_post(db, schemas.CreatePost(**posts)) q = db.query(models.Post) q = crud.filter_post_by_is_public(q) assert len(q.all()) == 0 db.close()
def test_update_post(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) category = crud.create_category(db, schemas.CreateCategory(**{"name": 'cat'})) category2 = crud.create_category(db, schemas.CreateCategory(**{"name": 'cat2'})) series = crud.create_series(db, schemas.CreateSeries(**{"name": 'ser'})) tag = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag'})) tag2 = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag2'})) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'category_id': category.id, 'series_id': series.id, 'tag_ids': [tag.id], } post = crud.create_post(db, schemas.CreatePost(**posts)) assert post.title == 'hoge' assert post.body == 'fuga' assert post.author_id == user.id assert post.category_id == category.id assert post.series_id == series.id assert post.tag[0] == tag new_posts = { 'title': 'hoge2', 'body': 'fuga2', 'public_at': '2020-05-17T00:00:00', 'category': category2.name, 'tags': [tag2.name], } new_post = schemas.UpdatePostReq(**new_posts) new_post = utils.UpdateIDPost(db, new_post) updated_post = crud.update_post(db, base_post=post, post=new_post) assert updated_post.title == 'hoge2' assert updated_post.body == 'fuga2' assert updated_post.author_id == user.id assert updated_post.category_id == category2.id assert updated_post.series_id == series.id assert updated_post.tag[0] == tag2 db.close()
def test_filter_by_series(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) series = crud.create_series(db, schemas.CreateSeries(**{"name": 'ser'})) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'series_id': series.id, } crud.create_post(db, schemas.CreatePost(**posts)) q = db.query(models.Post) q = crud.filter_post_by_series(q, series.id) assert len(q.all()) == 1 q = db.query(models.Post) q = crud.filter_post_by_series(q, series.id + 1) assert len(q.all()) == 0, 'filtering by category does not work' db.close()
def test_create_post(SessionLocal): db = SessionLocal() user = u_crud.create_user_query(db, u_schemas.UserCreate(**{"email": "foo", "name": "fooo", "password": "******"})) category = crud.create_category(db, schemas.CreateCategory(**{"name": 'cat'})) series = crud.create_series(db, schemas.CreateSeries(**{"name": 'ser'})) tag = crud.create_tag(db, schemas.CreateTag(**{"name": 'tag'})) posts = { 'title': 'hoge', 'body': 'fuga', 'public_at': '2020-05-17T00:00:00', 'author_id': user.id, 'category_id': category.id, 'series_id': series.id, 'tag_ids': [tag.id], } post = crud.create_post(db, schemas.CreatePost(**posts)) assert post.title == 'hoge' assert post.body == 'fuga' assert post.author_id == user.id assert post.category_id == category.id assert post.series_id == series.id assert post.tag[0] == tag db.close()