Skip to content

Commit 6cd3a09

Browse files
DaymasSTemmmmmo
andauthored
Fix double email reg issue (#215)
## Изменения Запрет на привязку почты (Email) к аккаунту у которого уже есть привязанная почта (Email) ## Детали реализации При попытке привязать почту к аккаунту юзера повторно (post email/registration с токеном сессии юзера ) вернётся код 409 Already exists (Поведение одинаковое, как для почты идентичной привязанной, так и для абсолютно новой почты) ## Check-List <!-- После сохранения у следующих полей появятся галочки, которые нужно проставить мышкой --> - [ ] Вы проверили свой код перед отправкой запроса? - [ ] Вы написали тесты к реализованным функциям? - [ ] Вы не забыли применить форматирование `black` и `isort` для _Back-End_ или `Prettier` для _Front-End_? --------- Co-authored-by: Morozov Artem <126605382+Temmmmmo@users.noreply.github.com>
1 parent e566f96 commit 6cd3a09

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

auth_backend/auth_plugins/email.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ async def _register(
229229
user = await cls._get_user(user_session=user_session, db_session=txn)
230230
if not user:
231231
raise SessionExpired(user_session.token)
232+
auth_method: AuthMethod | None = (
233+
AuthMethod.query(session=txn)
234+
.filter(AuthMethod.auth_method == Email.get_name(), AuthMethod.user_id == user.id)
235+
.first()
236+
)
237+
if auth_method:
238+
raise AlreadyExists(User, user.id)
232239
else:
233240
user = await cls._create_user(db_session=txn)
234241
method_params = await Email._add_to_db(user_inp, confirmation_token, user)

tests/test_routes/test_registration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,22 @@ def test_user_exists(client_auth: TestClient, dbsession: Session):
140140
dbsession.delete(row)
141141
dbsession.delete(dbsession.query(User).filter(User.id == db_user.user_id).one())
142142
dbsession.commit()
143+
144+
145+
def test_double_email_registration(client_auth: TestClient, dbsession: Session, user):
146+
user_id, body, response = user["user_id"], user["body"], user["login_json"]
147+
time = datetime.datetime.utcnow()
148+
body1 = {
149+
"email": body["email"],
150+
"password": "string",
151+
"scopes": [],
152+
"session_name": "name",
153+
}
154+
response = client_auth.post("/email/login", json=body1)
155+
token_ = response.json()['token']
156+
body2 = {"email": f"new{time}@email.com", "password": "random pwd"}
157+
body3 = {"email": body["email"], "password": "string"}
158+
response = client_auth.post(url, headers={"Authorization": token_}, json=body2)
159+
assert response.status_code == status.HTTP_409_CONFLICT
160+
response = client_auth.post(url, headers={"Authorization": token_}, json=body3)
161+
assert response.status_code == status.HTTP_409_CONFLICT

0 commit comments

Comments
 (0)