-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.py
95 lines (74 loc) · 2.73 KB
/
users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import hashlib
import os
import json
from passlib.apps import custom_app_context as pwd_context
import team
class User(object):
def __init__(self, username, teamNum):
self.username = username
self.team = team.teams.getTeam(teamNum)
class Users(object):
def __init__(self):
self.users = {}
if not os.path.exists('data'):
os.makedirs('data')
self.path = os.path.join('data', 'users.json')
try:
with open(self.path, 'r') as user_file:
self.users = json.loads(user_file.read())
except IOError:
pass
def save(self):
with open(self.path, 'w') as user_file:
json.dump(self.users, user_file)
# if username already exists, this overwrites it. So be careful upon calling it!
def add(self, username, team, password):
username = username.lower()
self.users[username] = {}
self.users[username]['password'] = pwd_context.hash(password)
self.users[username]['team'] = team.lower()
self.save()
def createUser(self, username, team, password):
username = username.lower()
try:
self.users[username]
return "please enter unique username"
except KeyError:
self.add(username, team, password)
def change_password(self, username, newpass):
self.users[username]['password'] = pwd_context.hash(newpass)
self.save()
def getUser(self, username, password):
if username and username in self.users and pwd_context.verify(
password, self.users[username]['password']):
return User(username, self.users[username]['team'])
else:
return None
# unit test
if __name__ == "__main__":
users = Users()
users.add('alan@randomsmiths.com', 'ftc16072', 'password')
# Check good login
user = users.getUser('alan@randomsmiths.com', 'password')
if not user:
print('Failure verifying good one')
print(f'Success w/ good one')
# Check bad password
user = users.getUser('alan@randomsmiths.com', 'badpass')
if user:
print("Shouldn't have validated")
# Check no account
user = users.getUser('noaccount', 'password')
if user:
print("Shouldn't have validated")
users.createUser('alan@randomsmiths.com', 'ftc16072', 'badpass')
# Check bad password
user = users.getUser('alan@randomsmiths.com', 'badpass')
if user:
print("Shouldn't have validated")
users.createUser('philip@randomsmiths.com', 'ftc16072', 'hahahaha')
user = users.getUser('philip@randomsmiths.com', 'hahahaha')
if user:
print('Success with creating')
else:
print('error creating user')