Skip to content

Commit

Permalink
393 replace cypress email tests with pytest and eliminate cypress (#490)
Browse files Browse the repository at this point in the history
* Replaced email cypress tests with new and adapted pytest tests (#393)

* Removed obsolete email cypress stuff (#393)

* Removed most remaining cypress stuff (#406)

* Removed node.js json files (#406)

* Upgraded and reapplied black (#393)

* Upgraded also black's dependencies (#393)
  • Loading branch information
cyplas authored Mar 6, 2023
1 parent ed9ef61 commit fa88811
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 2,276 deletions.
27 changes: 0 additions & 27 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ orbs:
azure-acr: circleci/[email protected]
node: circleci/[email protected]
python: circleci/[email protected]
cypress: cypress-io/cypress@1
codecov: codecov/[email protected]

jobs:
Expand Down Expand Up @@ -50,27 +49,6 @@ jobs:
file: /tmp/test-results/unit-tests/coverage.xml
- store_test_results:
path: /tmp/test-results/unit-tests
cypress:
# Set up test environment and run cypress tests
machine:
image: ubuntu-2004:202201-02
steps:
- checkout
- run:
name: Install Node Dependencies
command: npm i
- run:
name: Docker Compose Build
command: docker-compose -f docker/docker-compose.prod.yml build
- run:
name: Docker Compose Up
command: docker-compose -f docker/docker-compose.prod.yml up -d
- run:
name: Wait on Server
command: npx wait-on http://localhost:5000/questions
- run:
name: Cypress Run
command: npx cypress run
build_and_deploy_prod:
machine: true
steps:
Expand All @@ -97,9 +75,6 @@ workflows:
- pytest:
requires:
- lint
- cypress:
requires:
- lint
- build_and_deploy_prod:
context:
- azure
Expand All @@ -108,7 +83,6 @@ workflows:
only: master
requires:
- pytest
- cypress
- build_and_deploy_test:
context:
- azure
Expand All @@ -117,4 +91,3 @@ workflows:
only: develop
requires:
- pytest
- cypress
109 changes: 102 additions & 7 deletions app/account/tests/test_account_routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timedelta

from time import sleep
import pytest
import typing
from flask import url_for
Expand All @@ -19,8 +20,25 @@ def test_current_email(client):
with mock.patch("flask_jwt_extended.utils.get_current_user", return_value=user):
response = client.get(url_for("account.current_email"))
assert response.status_code == 401, "Unauthorized request"
assert response.json == {
"msg": 'Missing JWT in headers or cookies (Missing Authorization Header; Missing cookie "access_token")'
}

access_token = create_access_token(identity=user, fresh=True)
expiry_milliseconds = 1.0
access_token = create_access_token(
identity=user,
fresh=True,
expires_delta=timedelta(milliseconds=expiry_milliseconds),
)
client.set_cookie("localhost", "access_token", access_token)
sleep(0.001 * expiry_milliseconds)
response = client.get(url_for("account.current_email"))
assert response.status_code == 401, "Unauthorized request"
assert response.json == {"msg": "Token has expired"}

access_token = create_access_token(
identity=user, fresh=True, expires_delta=timedelta(seconds=60)
)
client.set_cookie("localhost", "access_token", access_token)
response = client.get(url_for("account.current_email"))
assert response.status_code == 200, "Is success"
Expand All @@ -29,15 +47,74 @@ def test_current_email(client):

@pytest.mark.integration
def test_update_email(client, accept_json):
response = client.put(url_for("account.update_email"), headers=accept_json)
assert response.status_code == 401, "Unauthorized request"

password = faker.password()
user = UsersFactory(password=password)
old_email = user.user_email
new_email = faker.email()
ok_data = {
"password": password,
"confirmEmail": new_email,
"newEmail": new_email,
}

access_token = create_access_token(identity=user, fresh=True)
response = client.put(
url_for("account.update_email"), headers=accept_json, json=ok_data
)
assert response.status_code == 401, "Unauthorized request"
assert response.json == {
"msg": 'Missing JWT in headers or cookies (Missing Authorization Header; Missing cookie "access_token")'
}
assert user.user_email == old_email

expiry_milliseconds = 1.0
access_token = create_access_token(
identity=user,
fresh=True,
expires_delta=timedelta(milliseconds=expiry_milliseconds),
)
client.set_cookie("localhost", "access_token", access_token)
new_email = faker.email()
sleep(0.001 * expiry_milliseconds)
response = client.put(
url_for("account.update_email"), headers=accept_json, json=ok_data
)
assert response.status_code == 401, "Unauthorized request"
assert response.json == {"msg": "Token has expired"}
assert user.user_email == old_email

access_token = create_access_token(
identity=user, fresh=True, expires_delta=timedelta(hours=1)
)
client.set_cookie("localhost", "access_token", access_token)
response = client.put(
url_for("account.update_email"), headers=accept_json, json=ok_data
)
assert response.status_code == 200, "Email changed successfully"
assert user.user_email == new_email

response_login_with_old_email = client.post(
url_for("auth.login"),
json={
"email": old_email,
"password": password,
},
)
assert (
response_login_with_old_email.status_code == 401
), "Login with old email denied"
assert response_login_with_old_email.json == {
"error": "Wrong email or password. Try again."
}

response_login_with_new_email = client.post(
url_for("auth.login"),
json={
"email": new_email,
"password": password,
},
)
assert (
response_login_with_new_email.status_code == 200
), "Login with new email successful"

response = client.put(
url_for("account.update_email"),
Expand All @@ -48,7 +125,10 @@ def test_update_email(client, accept_json):
"newEmail": new_email,
},
)
assert response.status_code == 200, "Email changed successfully"
assert response.status_code == 409, "Cannot change email to itself"
assert response.json == {
"error": "Cannot update email. Email already exists in the database."
}

response = client.put(
url_for("account.update_email"),
Expand All @@ -60,6 +140,7 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 400, "Password is required"
assert response.json == {"error": "password must be included in the request body."}

response = client.put(
url_for("account.update_email"),
Expand All @@ -71,6 +152,9 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 400, "Confirm email is required"
assert response.json == {
"error": "confirmEmail must be included in the request body."
}

response = client.put(
url_for("account.update_email"),
Expand All @@ -82,6 +166,7 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 400, "New email is required"
assert response.json == {"error": "newEmail must be included in the request body."}

invalid_email = "invalid,@email"
response = client.put(
Expand All @@ -94,6 +179,9 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 400, "Email is invalid"
assert response.json == {
"error": "Cannot update email. Email is not formatted correctly."
}

response = client.put(
url_for("account.update_email"),
Expand All @@ -105,6 +193,7 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 401, "Wrong password"
assert response.json == {"error": "Cannot update email. Incorrect password."}

response = client.put(
url_for("account.update_email"),
Expand All @@ -116,6 +205,9 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 400, "Emails should be equal"
assert response.json == {
"error": "Cannot update email. New email address and confirm new email address do not match."
}

another_user = UsersFactory()
response = client.put(
Expand All @@ -128,6 +220,9 @@ def test_update_email(client, accept_json):
},
)
assert response.status_code == 409, "Email is not unique"
assert response.json == {
"error": "Cannot update email. Email already exists in the database."
}


@pytest.mark.integration
Expand Down
12 changes: 0 additions & 12 deletions cypress.config.js

This file was deleted.

3 changes: 0 additions & 3 deletions cypress/cypress.json

This file was deleted.

Loading

0 comments on commit fa88811

Please sign in to comment.