Exemplo n.º 1
0
def user(uid: int):
    if request.method == 'GET':
        return UserHandler().get_user_by_id(uid)
    elif request.method == 'DELETE':
        return UserHandler().delete_user_by_id(uid)
    else:
        return jsonify(Error="Method not allowed."), 405
Exemplo n.º 2
0
    def setUp(self):
        self.user1 = [
            1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899',
            'Carolina', .99
        ]
        self.user2 = [
            2, 'Javier', 'L', 'morsagmail.com', '787888999', 'Uganda', .23
        ]
        self.user3 = [
            3, 'Morsa', 'TryHard22', '*****@*****.**', '939-787-7799',
            'Ivory Coast', .75
        ]
        self.user4 = [
            4, 'Walrus', 'Paul', '*****@*****.**', '787/123/4567', 'Maya',
            -.99
        ]
        self.user5 = [
            5, 'Morsa', '', '*****@*****.**', '7844445599', 'Quebra', .99
        ]
        self.user6 = [
            6, '', 'lol', '*****@*****.**', '7844445599', 'Quebra', .99
        ]
        self.user7 = [
            7, 'Juan', 'lol', '*****@*****.**', '7844445599', '22', .99
        ]

        self.uh = UserHandler()
        self.dao = UserDAO()
        self.new_user = {
            "uusername": str(random.randint(1000, 10000)),
            "upassword": "******",
            "uemail": "*****@*****.**",
            "uphone": str(1231231234)
        }
        self.app = Flask(__name__)
Exemplo n.º 3
0
def users():
    if request.method == 'GET':
        return UserHandler().get_all_users()
    if request.method == 'POST':
        return UserHandler().insert_user(request.json)
    else:
        return jsonify(Error="Method not allowed."), 405
Exemplo n.º 4
0
def register():
    if request.method == 'GET':
        return render_template("register.html")
    if request.method == 'POST':
        username = request.json['uusername']
        password = request.json['upassword']
        UserHandler().do_register(request.json)
        if UserHandler().do_login(username, password):
            return jsonify(signedIn=True)
def register():
    if request.method == 'GET':
        return render_template("register.html")
    if request.method == 'POST':
        username = request.json['uusername']
        password = request.json['upassword']
        UserHandler().do_register(request.json)
        if UserHandler().do_login(username, password):
            flash(f'Account created for {username}!', 'success')
            return redirect(url_for('Request_feed'))
        return render_template('register.html')
Exemplo n.º 6
0
 def tearDown(self):
     with self.app as c:
         rv = c.post('/HTH/login',
                     json={
                         "uusername": self.new_user['uusername'],
                         "upassword": self.new_user['upassword']
                     })
         if session['logged_in']:
             uid = session['uid']
             UserHandler().delete_user_by_id(uid)
def user_login():
    if request.method == 'GET':
        return render_template("login.html")

    if request.method == 'POST':
        username = request.json['username']
        password = request.json['password']
        if UserHandler().do_login(username, password):
            return jsonify(logged_in=True, username=username)
        else:

            return jsonify(logged_in=False)
Exemplo n.º 8
0
def profile():
    if session['logged_in']:
        if request.method == 'GET':
            user_info = UserHandler().get_user_by_id(session['uid'])
            unf_req = RequestHandler().get_requests_by_user_status(
                session['uid'], 'fuf')
            inprog_req = RequestHandler().get_requests_by_user_status(
                session['uid'], 'unfuf')
            fufld_req = RequestHandler().get_requests_by_user_status(
                session['uid'], 'pending')
            return render_template("userProfile.html",
                                   Info=user_info,
                                   Unf=unf_req,
                                   Inp=inprog_req,
                                   Fuf=fufld_req)

    else:
        return redirect(url_for('user_login'))
Exemplo n.º 9
0
def user_logout():
    if request.method == 'GET':
        if UserHandler().do_logout():
            return redirect(url_for('home'))
Exemplo n.º 10
0
class UserHandlerTestCase(unittest.TestCase):
    # unit tests for validating user operations
    def setUp(self):
        self.user1 = [
            1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899',
            'Carolina', .99
        ]
        self.user2 = [
            2, 'Javier', 'L', 'morsagmail.com', '787888999', 'Uganda', .23
        ]
        self.user3 = [
            3, 'Morsa', 'TryHard22', '*****@*****.**', '939-787-7799',
            'Ivory Coast', .75
        ]
        self.user4 = [
            4, 'Walrus', 'Paul', '*****@*****.**', '787/123/4567', 'Maya',
            -.99
        ]
        self.user5 = [
            5, 'Morsa', '', '*****@*****.**', '7844445599', 'Quebra', .99
        ]
        self.user6 = [
            6, '', 'lol', '*****@*****.**', '7844445599', 'Quebra', .99
        ]
        self.user7 = [
            7, 'Juan', 'lol', '*****@*****.**', '7844445599', '22', .99
        ]

        self.uh = UserHandler()
        self.dao = UserDAO()
        self.new_user = {
            "uusername": str(random.randint(1000, 10000)),
            "upassword": "******",
            "uemail": "*****@*****.**",
            "uphone": str(1231231234)
        }
        self.app = Flask(__name__)

    def test_validUser(self):
        self.assertTrue(self.uh.validateUser(self.user1))
        self.assertFalse(self.uh.validateUser(self.user2))
        self.assertTrue(self.uh.validateUser(self.user3))
        self.assertFalse(self.uh.validateUser(self.user4))
        self.assertFalse(self.uh.validateUser(self.user5))
        self.assertFalse(self.uh.validateUser(self.user6))
        self.assertFalse(self.uh.validateUser(self.user7))

    def test_validUserJSON(self):
        user1JSON = self.uh.createUserDict(self.user1)
        user2JSON = self.uh.createUserDict(self.user2)
        user3JSON = self.uh.createUserDict(self.user3)
        user4JSON = self.uh.createUserDict(self.user4)
        user5JSON = self.uh.createUserDict(self.user5)
        user6JSON = self.uh.createUserDict(self.user6)
        user7JSON = self.uh.createUserDict(self.user7)

        self.assertTrue(self.uh.validateUserJSON(user1JSON))
        self.assertFalse(self.uh.validateUserJSON(user2JSON))
        self.assertTrue(self.uh.validateUserJSON(user3JSON))
        self.assertFalse(self.uh.validateUserJSON(user4JSON))
        self.assertFalse(self.uh.validateUserJSON(user5JSON))
        self.assertFalse(self.uh.validateUserJSON(user6JSON))
        self.assertFalse(self.uh.validateUserJSON(user7JSON))

    def test_get_all_users(self):
        # will get a list of users
        with self.app.app_context():
            result = json.loads(self.uh.get_all_users().get_data())['Users']
            self.assertTrue(len(result) > 1)

    def test_get_user_by_id(self):
        with self.app.app_context():
            result = json.loads(self.uh.get_all_users().get_data())['Users']
            first_user = result[0]
            user_result = json.loads(
                self.uh.get_user_by_id(first_user['uid']).get_data())['User']
            self.assertEqual(user_result, first_user)
            self.assertEqual(self.uh.get_user_by_id(-1)[1], 404)

    def test_insert_user(self):
        with self.app.app_context():
            result = self.uh.insert_user(self.new_user)
            uid = json.loads(result[0].get_data())['User']['uid']
            self.assertEqual(result[1], 201)
            self.dao.delete_user_by_id(uid)  # so test user is not persisted

            self.new_user.pop('uusername')
            result2 = self.uh.insert_user(self.new_user)
            self.assertEqual(result2[1], 400)  # user should never enter db

    # def test_do_logout(self):
    #     with self.app.app_context():
    #         self.assertTrue(self.uh.do_logout())
    #         self.assertFalse(session['logged_in'])

    def test_do_register(self):
        with self.app.app_context():
            # similar to the insert_user method
            result = self.uh.do_register(self.new_user)
            uid = json.loads(result[0].get_data())['User']['uid']
            self.assertEqual(result[1], 201)
            self.dao.delete_user_by_id(uid)  # so test user is not persisted

            self.new_user.pop('uusername')
            result2 = self.uh.do_register(self.new_user)
            self.assertEqual(result2[1], 400)  # user should never enter db

    def test_do_login(self):
        with self.app.app_context():
            # create new user
            curr_pass = self.new_user['upassword']
            result = self.uh.do_register(self.new_user)[0].json
            print(result)
            uid = result['User']['uid']

            # test right password
            self.assertTrue(
                self.uh.do_login(self.new_user['uusername'],
                                 curr_pass,
                                 testing=True))
            # self.assertTrue(session['logged_in'])

            # test wrong password
            # self.uh.do_logout()
            self.assertFalse(
                self.uh.do_login(self.new_user['uusername'],
                                 curr_pass + 'LOL',
                                 testing=True))

            # delete test user
            self.dao.delete_user_by_id(uid)  # so test user is not persisted
def login():
    if request.method == 'GET':
        return UserHandler().check_login(request.json)
    else:
        return jsonify(Error="Method not allowed."), 405