vishalbhat07/ctfd
0
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3 4from CTFd.models import Users5from CTFd.utils.crypto import verify_password6from tests.helpers import create_ctfd, destroy_ctfd, login_as_user, register_user7 8 9def test_user_set_profile():10 """Test that a user can set and remove their information in their profile"""11 app = create_ctfd()12 with app.app_context():13 register_user(app)14 client = login_as_user(app)15 16 data = {17 "name": "user",18 "email": "user@examplectf.com",19 "confirm": "",20 "password": "",21 "affiliation": "affiliation_test",22 "website": "https://examplectf.com",23 "country": "US",24 }25 26 r = client.patch("/api/v1/users/me", json=data)27 assert r.status_code == 20028 29 user = Users.query.filter_by(id=2).first()30 assert user.affiliation == data["affiliation"]31 assert user.website == data["website"]32 assert user.country == data["country"]33 34 r = client.get("/settings")35 resp = r.get_data(as_text=True)36 for _k, v in data.items():37 assert v in resp38 39 data = {40 "name": "user",41 "email": "user@examplectf.com",42 "confirm": "",43 "password": "",44 "affiliation": "",45 "website": "",46 "country": "",47 }48 49 r = client.patch("/api/v1/users/me", json=data)50 assert r.status_code == 20051 52 user = Users.query.filter_by(id=2).first()53 assert user.affiliation == data["affiliation"]54 assert user.website == data["website"]55 assert user.country == data["country"]56 destroy_ctfd(app)57 58 59def test_user_can_change_password():60 """Test that a user can change their password and is prompted properly"""61 app = create_ctfd()62 with app.app_context():63 register_user(app)64 client = login_as_user(app)65 66 data = {67 "name": "user",68 "email": "user@examplectf.com",69 "confirm": "",70 "password": "new_password",71 "affiliation": "",72 "website": "",73 "country": "",74 }75 76 r = client.patch("/api/v1/users/me", json=data)77 user = Users.query.filter_by(id=2).first()78 assert verify_password(data["password"], user.password) is False79 assert r.status_code == 40080 assert r.get_json() == {81 "errors": {"confirm": ["Please confirm your current password"]},82 "success": False,83 }84 85 data["confirm"] = "wrong_password"86 87 r = client.patch("/api/v1/users/me", json=data)88 user = Users.query.filter_by(id=2).first()89 assert verify_password(data["password"], user.password) is False90 assert r.status_code == 40091 assert r.get_json() == {92 "errors": {"confirm": ["Your previous password is incorrect"]},93 "success": False,94 }95 96 data["confirm"] = "password"97 r = client.patch("/api/v1/users/me", json=data)98 assert r.status_code == 20099 user = Users.query.filter_by(id=2).first()100 assert verify_password(data["password"], user.password) is True101 destroy_ctfd(app)102 