<reponame>ftc16072/Scouting
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('<EMAIL>', '<PASSWORD>', 'password')

    # Check good login
    user = users.getUser('<EMAIL>', 'password')
    if not user:
        print('Failure verifying good one')
    print(f'Success w/ good one')

    # Check bad password
    user = users.getUser('<EMAIL>', '<PASSWORD>')
    if user:
        print("Shouldn't have validated")
    # Check no account
    user = users.getUser('noaccount', 'password')
    if user:
        print("Shouldn't have validated")

    users.createUser('<EMAIL>', 'ftc16072', 'badpass')
     # Check bad password
    user = users.getUser('<EMAIL>', 'bad<PASSWORD>')
    if user:
        print("Shouldn't have validated")
    
    users.createUser('<EMAIL>', 'ftc16072', 'hahahaha')

    user = users.getUser('<EMAIL>', 'hahahaha')
    if user:
        print('Success with creating')
    else:
        print('error creating user')

    
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('<EMAIL>', '<PASSWORD>', 'password')

    # Check good login
    user = users.getUser('<EMAIL>', 'password')
    if not user:
        print('Failure verifying good one')
    print(f'Success w/ good one')

    # Check bad password
    user = users.getUser('<EMAIL>', '<PASSWORD>')
    if user:
        print("Shouldn't have validated")
    # Check no account
    user = users.getUser('noaccount', 'password')
    if user:
        print("Shouldn't have validated")

    users.createUser('<EMAIL>', 'ftc16072', 'badpass')
     # Check bad password
    user = users.getUser('<EMAIL>', 'bad<PASSWORD>')
    if user:
        print("Shouldn't have validated")
    
    users.createUser('<EMAIL>', 'ftc16072', 'hahahaha')

    user = users.getUser('<EMAIL>', 'hahahaha')
    if user:
        print('Success with creating')
    else:
        print('error creating user')
# if username already exists, this overwrites it.   So be careful upon calling it! # unit test # Check good login # Check bad password # Check no account # Check bad password