Feat(API): members CRUD functionalities
- Added tests for members flow (creating/deleting/reset) - Various fixes to tooling
This commit is contained in:
		
				
					committed by
					
						
						Minh Tran Nhat
					
				
			
			
				
	
			
			
			
						parent
						
							026be1328e
						
					
				
				
					commit
					3c78fe9133
				
			
							
								
								
									
										66
									
								
								backend/tests/blueprints/test_members.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								backend/tests/blueprints/test_members.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
import pytest
 | 
			
		||||
from freezegun import freeze_time
 | 
			
		||||
from itsdangerous import URLSafeTimedSerializer
 | 
			
		||||
from quart import Quart
 | 
			
		||||
 | 
			
		||||
from backend.blueprints.members import EMAIL_VERIFICATION_SALT
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_register(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
 | 
			
		||||
    test_client = app.test_client()
 | 
			
		||||
    data = {
 | 
			
		||||
        "email": "new@todo.minhtrannhat.com",
 | 
			
		||||
        "password": "testPassword2$",
 | 
			
		||||
    }
 | 
			
		||||
    await test_client.post("/members/", json=data)
 | 
			
		||||
    response = await test_client.post("/sessions/", json=data)
 | 
			
		||||
    assert response.status_code == 200
 | 
			
		||||
    assert "Sending welcome.html to new@todo.minhtrannhat.com" in caplog.text
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.mark.parametrize(
 | 
			
		||||
    "time, expected",
 | 
			
		||||
    [("2022-01-01", 403), (None, 200)],
 | 
			
		||||
)
 | 
			
		||||
async def test_verify_email(app: Quart, time: str | None, expected: int) -> None:
 | 
			
		||||
    with freeze_time(time):
 | 
			
		||||
        signer = URLSafeTimedSerializer(app.secret_key, salt=EMAIL_VERIFICATION_SALT)
 | 
			
		||||
        token = signer.dumps(1)
 | 
			
		||||
    test_client = app.test_client()
 | 
			
		||||
    response = await test_client.put("/members/email/", json={"token": token})
 | 
			
		||||
    assert response.status_code == expected
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_verify_email_invalid_token(app: Quart) -> None:
 | 
			
		||||
    test_client = app.test_client()
 | 
			
		||||
    response = await test_client.put("/members/email/", json={"token": "invalid"})
 | 
			
		||||
    assert response.status_code == 400
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_change_password(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
 | 
			
		||||
    test_client = app.test_client()
 | 
			
		||||
    data = {
 | 
			
		||||
        "email": "new_password@todo.minhtrannhat.com",
 | 
			
		||||
        "password": "testPassword2$",
 | 
			
		||||
    }
 | 
			
		||||
    response = await test_client.post("/members/", json=data)
 | 
			
		||||
    async with test_client.authenticated("2"):  # type: ignore
 | 
			
		||||
        response = await test_client.put(
 | 
			
		||||
            "/members/password/",
 | 
			
		||||
            json={
 | 
			
		||||
                "currentPassword": data["password"],
 | 
			
		||||
                "newPassword": "testPassword3$",
 | 
			
		||||
            },
 | 
			
		||||
        )
 | 
			
		||||
        assert response.status_code == 200
 | 
			
		||||
    assert "Sending password_changed.html to new@todo.minhtrannhat.com" in caplog.text
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_forgotten_password(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
 | 
			
		||||
    test_client = app.test_client()
 | 
			
		||||
    data = {"email": "member@todo.minhtrannhat.com"}
 | 
			
		||||
    response = await test_client.put("/members/forgotten-password/", json=data)
 | 
			
		||||
    assert response.status_code == 200
 | 
			
		||||
    assert (
 | 
			
		||||
        "Sending forgotten_password.html to member@todo.minhtrannhat.com" in caplog.text
 | 
			
		||||
    )
 | 
			
		||||
		Reference in New Issue
	
	Block a user