44
55from datetime import datetime , timedelta
66import os
7+ import time
78import pytest
89import json
910from flask import url_for
1213from unittest .mock import patch
1314
1415from mergin .tests import test_workspace
15-
16+ from .. auth . app import generate_confirmation_token , confirm_token
1617from ..auth .models import User , UserProfile , LoginHistory
1718from ..auth .tasks import anonymize_removed_users
1819from ..app import db
@@ -152,26 +153,20 @@ def test_user_register(client, username, email, pwd, expected):
152153
153154
154155def test_confirm_email (app , client ):
155- serializer = URLSafeTimedSerializer (app .config ["SECRET_KEY" ])
156- token = serializer .dumps (
157- "mergin@mergin.com" , salt = app .config ["SECURITY_PASSWORD_SALT" ]
158- )
159- resp = client .post (url_for ("/.mergin_auth_controller_confirm_email" , token = token ))
160- assert resp .status_code == 200
161-
162156 user = User .query .filter_by (username = "mergin" ).first ()
163- # tests with old registered user
157+ token = generate_confirmation_token (
158+ app , user .email , app .config ["SECURITY_EMAIL_SALT" ]
159+ )
164160 user .verified_email = False
165- user .registration_date = datetime .utcnow () - timedelta (days = 1 )
166161 db .session .commit ()
167- resp = client .post (url_for ("/.mergin_auth_controller_confirm_email" , token = token ))
168- assert resp .status_code == 200
169162
170- # try again with freshly registered user
171- user .verified_email = False
172- user .registration_date = datetime .utcnow ()
173- db .session .add (user )
174- db .session .commit ()
163+ # verify token can't be used in different context
164+ resp = client .post (
165+ url_for ("/.mergin_auth_controller_confirm_new_password" , token = token ),
166+ json = {"password" : "ilovemergin#0" , "confirm" : "ilovemergin#0" },
167+ )
168+ assert resp .status_code == 400
169+
175170 resp = client .post (url_for ("/.mergin_auth_controller_confirm_email" , token = token ))
176171 assert resp .status_code == 200
177172
@@ -187,21 +182,35 @@ def test_confirm_email(app, client):
187182 resp = client .post (
188183 url_for (
189184 "/.mergin_auth_controller_confirm_email" ,
190- token = serializer . dumps (
191- "tests@mergin.com" , salt = app .config ["SECURITY_PASSWORD_SALT " ]
185+ token = generate_confirmation_token (
186+ app , "tests@mergin.com" , app .config ["SECURITY_EMAIL_SALT " ]
192187 ),
193188 )
194189 )
195190 assert resp .status_code == 404
196191
192+ # test expired token
193+ token = generate_confirmation_token (
194+ app , user .email , app .config ["SECURITY_EMAIL_SALT" ]
195+ )
196+ time .sleep (2 )
197+ assert not confirm_token (
198+ token = token , expiration = 1 , salt = app .config ["SECURITY_EMAIL_SALT" ]
199+ )
200+
197201
198202def test_confirm_password (app , client ):
199- serializer = URLSafeTimedSerializer ( app . config [ "SECRET_KEY" ] )
200- token = serializer . dumps (
201- "mergin@mergin.com" , salt = app .config ["SECURITY_PASSWORD_SALT" ]
203+ user = User . query . filter_by ( username = "mergin" ). first ( )
204+ token = generate_confirmation_token (
205+ app , user . email , app .config ["SECURITY_PASSWORD_SALT" ]
202206 )
203207
204208 form_data = {"password" : "ilovemergin#0" , "confirm" : "ilovemergin#0" }
209+
210+ # verify token can't be used in different context
211+ resp = client .post (url_for ("/.mergin_auth_controller_confirm_email" , token = token ))
212+ assert resp .status_code == 400
213+
205214 resp = client .post (
206215 url_for ("/.mergin_auth_controller_confirm_new_password" , token = token ),
207216 data = json .dumps (form_data ),
@@ -221,8 +230,8 @@ def test_confirm_password(app, client):
221230 resp = client .post (
222231 url_for (
223232 "/.mergin_auth_controller_confirm_new_password" ,
224- token = serializer . dumps (
225- "tests@mergin.com" , salt = app .config ["SECURITY_PASSWORD_SALT" ]
233+ token = generate_confirmation_token (
234+ app , "tests@mergin.com" , app .config ["SECURITY_PASSWORD_SALT" ]
226235 ),
227236 ),
228237 data = json .dumps (form_data ),
@@ -240,8 +249,8 @@ def test_confirm_password(app, client):
240249 resp = client .post (
241250 url_for (
242251 "/.mergin_auth_controller_confirm_new_password" ,
243- token = serializer . dumps (
244- "tests@mergin.com" , salt = app .config ["SECURITY_PASSWORD_SALT" ]
252+ token = generate_confirmation_token (
253+ app , "tests@mergin.com" , app .config ["SECURITY_PASSWORD_SALT" ]
245254 ),
246255 )
247256 )
0 commit comments