def test_auth_refresh_multiple(freezer, client, user): user, _ = user freezer.tick(datetime.timedelta(**settings.JWT_ACCESS_LIFESPAN) + datetime.timedelta(seconds=1)) response = client.post("/api/v1.0/auth/refresh/", headers={"Authorization": f'Bearer {user["access_token"]}'}) assert_that(response, matchers.has_status(200)) token_data = response.json response = client.get( f'/api/v1.0/user/{user["id"]}/', headers={"Authorization": f'Bearer {token_data["access_token"]}'} ) assert_that(response, matchers.has_status(200)) freezer.tick(datetime.timedelta(**settings.JWT_ACCESS_LIFESPAN) + datetime.timedelta(seconds=1)) response = client.post("/api/v1.0/auth/refresh/", headers={"Authorization": f'Bearer {token_data["access_token"]}'}) assert_that(response, matchers.has_status(200)) token_data = response.json response = client.get( f'/api/v1.0/user/{user["id"]}/', headers={"Authorization": f'Bearer {token_data["access_token"]}'} ) assert_that(response, matchers.has_status(200))
def test_auth_logout(client, user): user, _ = user response = client.get(f'/api/v1.0/user/{user["id"]}/', headers={"Authorization": f'Bearer {user["access_token"]}'}) assert_that(response, matchers.has_status(200)) response = client.post("/api/v1.0/auth/logout/", headers={"Authorization": f'Bearer {user["access_token"]}'}) assert_that(response, matchers.has_status(204)) response = client.get(f'/api/v1.0/user/{user["id"]}/', headers={"Authorization": f'Bearer {user["access_token"]}'}) assert_that(response, matchers.has_status(403))
def test_auth_login(client, user): user, password = user credentials = {"email": user["email"], "password": password} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(200))
def test_verification_complete_invalid(client, verification_type, recipient): response = client.post( f"/api/v1.0/verification/{verification_type}/confirm/", json={ "phone": recipient, "code": "1234" }) assert_that(response, matchers.has_status(400)) response = client.post( f"/api/v1.0/verification/{verification_type}/confirm/", json={ "phone": recipient, "code": "1111" }) assert_that(response, matchers.has_status(200))
def test_get_roles(client, admin, user): admin, _ = admin user, _ = user response = client.get( f'/api/v1.0/user/{admin["id"]}/role/', headers={"Authorization": f'Bearer {admin["access_token"]}'}) assert_that(response, matchers.has_status(200)) assert_that(response.data.decode(), matchers.is_json(has_items(*admin["roles"]))) response = client.get( f'/api/v1.0/user/{user["id"]}/role/', headers={"Authorization": f'Bearer {admin["access_token"]}'}) assert_that(response, matchers.has_status(200)) assert_that(response.data.decode(), matchers.is_json(is_not(has_items(*admin["roles"]))))
def admin(client): response = client.post( "/api/v1.0/auth/login/", json={ "email": settings.PROVISIONING_ADMIN_EMAIL, "password": settings.PROVISIONING_ADMIN_PASSWORD }, ) assert_that(response, matchers.has_status(200)) access_token = response.json["access_token"] response = client.get("/api/v1.0/user/current/", headers={"Authorization": f"Bearer {access_token}"}) assert_that(response, matchers.has_status(200)) user = response.json user["access_token"] = access_token return (user, settings.PROVISIONING_ADMIN_PASSWORD)
def test_set_password(client, user): user, old_password = user new_password = "******" response = client.post( "/api/v1.0/user/current/set_password/", json={"password": new_password}, headers={"Authorization": f'Bearer {user["access_token"]}'}, ) assert_that(response, matchers.has_status(200)) credentials = {"email": user["email"], "password": old_password} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(401)) credentials = {"email": user["email"], "password": new_password} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(200))
def test_password_reset(client, user, requests_mock): requests_mock.post(f"{settings.EMAIL_API_ROOT_URL}template/password_reset/send/", status_code=200) user, old_password = user response = client.post("/api/v1.0/password/reset/", json={"email": user["email"], "url_template": "{token}"}) assert_that(response, matchers.has_status(200)) password_reset_email_data = requests_mock.last_request.json() assert_that(password_reset_email_data, has_entries({"variables": has_key("password_reset_url")})) token = password_reset_email_data["variables"]["password_reset_url"] new_password = "******" response = client.post("/api/v1.0/password/reset/confirm/", json={"token": token, "new_password": new_password}) credentials = {"email": user["email"], "password": old_password} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(401)) credentials = {"email": user["email"], "password": new_password} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(200))
def user(client): password = "******" response = client.post( "/api/v1.0/user/", json={ "email": f"test-{uuid.uuid4()}@example.com", "phone": "+1234567890", "first_name": "晓鹏", "last_name": "郑", "password": password, }, ) assert_that(response, matchers.has_status(201)) return (response.json, password)
def test_template_send(client, message): with mail.record_messages() as outbox: response = client.post('/api/v1.0/template/password_reset/send/', json={ 'from_': '*****@*****.**', 'to': ['*****@*****.**'], 'subject': 'Hello', 'tag': 'test', 'variables': {} }) assert_that(response, matchers.has_status(201)) assert_that( outbox[0], has_properties(subject='Hello', sender=message['from_'], recipients=message['to']))
def test_send(client, message): with mail.record_messages() as outbox: response = client.post('/api/v1.0/message/', json={ 'from_': '*****@*****.**', 'to': ['*****@*****.**'], 'subject': 'Hello', 'html': '', 'text': '', 'tag': 'test' }) assert_that(response, matchers.has_status(201)) assert_that( outbox[0], has_properties(subject='Hello', sender=message['from_'], recipients=message['to']))
def test_user_utf8mb4(client): first_name = "晓鹏" last_name = "郑" response = client.post( "/api/v1.0/user/", json={ "email": f"test-{uuid.uuid4()}@investex.com", "phone": "+1234567890", "first_name": first_name, "last_name": last_name, "password": None, }, ) assert_that(response, matchers.has_status(201)) assert_that( response.data.decode(), matchers.is_json( has_entries(first_name=equal_to(first_name), last_name=equal_to(last_name))), )
def test_feedback_ses_confirmation(client): response = client.post( path='/feedback/ses/', headers={ 'x-amz-sns-message-type': 'SubscriptionConfirmation', 'x-amz-sns-message-id': '165545c9-2a5c-472c-8df2-7ff2be2b3b1b', 'x-amz-sns-topic-arn': 'arn:aws:sns:us-west-2:123456789012:MyTopic', 'Content-Length': '1336', 'Content-Type': 'text/plain; charset=UTF-8', 'Connection': 'Keep-Alive', 'User-Agent': 'Amazon Simple Notification Service Agent' }, json={ 'Type': 'SubscriptionConfirmation', 'MessageId': '165545c9-2a5c-472c-8df2-7ff2be2b3b1b', 'Token': '2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736', # noqa 'TopicArn': 'arn:aws:sns:us-west-2:123456789012:MyTopic', 'Message': 'You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\n' 'To confirm the subscription, visit the SubscribeURL included in this message.', 'SubscribeURL': 'https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736', # noqa 'Timestamp': '2012-04-26T20:45:04.751Z', 'SignatureVersion': '1', 'Signature': 'EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=', # noqa 'SigningCertURL': 'https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem' # noqa }) assert_that(response, matchers.has_status(200))
def test_template_preview(client): response = client.get('/template/password_reset.txt/preview/') assert_that(response, matchers.has_status(200))
def test_verification(client, verification_type, recipient): response = client.post(f"/api/v1.0/verification/{verification_type}/", json={"phone": recipient}) assert_that(response, matchers.has_status(200))
def test_auth_verify_token(client, user): user, _ = user response = client.get("/api/v1.0/auth/verify_token/", headers={"Authorization": f'Bearer {user["access_token"]}'}) assert_that(response, matchers.has_status(200))
def test_auth_login_failed(client): credentials = {"email": "*****@*****.**", "password": "******"} response = client.post("/api/v1.0/auth/login/", json=credentials) assert_that(response, matchers.has_status(401))
def test_feedback_ses(client): response = client.get('/feedback/ses/') assert_that(response, matchers.has_status(200))
def test_send(client, phone): response = client.post("/api/v1.0/sms/", json={"user_id": "0", "from": "AFFO", "phone": phone, "message": "test"}) assert_that(response, matchers.has_status(201))