def insert_user(self, json_input):
     if len(json_input
            ) != 4:  # check if there are sufficient elements in input
         return jsonify(Error="Malformed insert user request"), 400
     try:  # check parameters are valid
         uusername = json_input['uusername']
         upassword = json_input['upassword']
         uemail = json_input['uemail']
         uphone = json_input['uphone']
     except:
         return jsonify(
             Error="Unexpected attributes in insert user request"), 400
     try:
         if uusername and upassword and uemail and uphone:
             dao = UserDAO()
             # if dao.get_user_by_email(uemail):  # checks if email exists in database. EMAILS MUST BE UNIQUE.
             #     return jsonify(Error="User with that email already exists. Please try a different one."), 400
             # elif dao.get_user_by_username(uusername):  # same but with username.
             #     return jsonify(Error="User with that username already exists. Please try a different one."), 400
             uid = dao.insert_user(uusername, upassword, uemail, uphone)
         else:
             return jsonify(Error="One or more attribute is empty"), 400
     except:
         return jsonify(Error="User insertion failed horribly."), 400
     try:
         LoginDAO().insert_login(uusername, upassword, uid)
     except:
         return jsonify(Error="Login insertion failed horribly."), 400
     # Finally returns an user dict of the inserted user.
     return jsonify(User=self.createUserDict(
         [uid, uusername, upassword, uemail, uphone])), 201
예제 #2
0
 def setUp(self):
     self.r1 = [
         1, 'Toilet Paper', 'this is NOT a description', 'home',
         'Requesting', 1
     ]
     self.r2 = [
         2, 'Hand Sanitizer', 'this MAY be a decription', 'Maya',
         'Provided', 3
     ]
     self.r3 = [
         3, 'Food', 'this is POSSIBLY a description', 'laCalle', 'Provided',
         1
     ]
     self.r4 = [
         4, 'send help pls', 'this is ME DOING a description', 'home',
         'Requesting', 3
     ]
     self.r5 = [
         5, 'beer', 'this is JUST a description', 'bar', 'Requesting', 4
     ]
     self.requests: list = [self.r1, self.r2, self.r3, self.r4, self.r5]
     self.user1 = [1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899']
     self.keys = [
         'rid', 'rtitle', 'rdescription', 'rlocation', 'rstatus', 'ruser'
     ]
     self.rh = RequestHandler()
     self.dao = RequestDAO()
     self.daoUSER = UserDAO()
     self.app = Flask(__name__)
예제 #3
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__)
 def delete_user_by_id(self, uid: int):
     try:
         row = UserDAO().get_user_by_id(uid)
         if not row:
             return jsonify(Error="User " + str(uid) + " not found."), 404
         else:
             if UserDAO().delete_user_by_id(uid) > 0:
                 return jsonify(DeletedUser=self.createUserDict(row)), 200
             else:
                 return jsonify(Error="Delete failed"), 404
     except Exception as e:
         print(e)
         return jsonify(ERROR=e), 500
예제 #5
0
    def do_login(username, password):
        try:
            dao = UserDAO()
            user = dao.get_user_by_username(username)
            uid = user[0]  #assuming that the uid is the first field in the row
            db_pass = json.loads(
                UserHandler().get_user_by_id(uid))['upassword']

            if user and sha256_crypt.verify(password, db_pass):
                session['logged_in'] = True
                session['uid'] = uid
                return True
            return False
        except:
            flash('Error on login')
            return False
 def get_user_by_id(self, uid: int):
     try:
         user = UserDAO().get_user_by_id(uid)
         if user:
             return jsonify(User=self.createUserDict(user))
         else:
             return jsonify(ERROR="User Not Found"), 404
     except:
         return jsonify(ERROR="Handler Error"), 500
 def get_all_users(self):
     try:
         users = UserDAO().get_all_users()
         results = list()
         for row in users:
             results.append(self.createUserDict(row))
         return jsonify(Users=results)
     except:
         return jsonify(ERROR="Server error!"), 500
 def do_login(self, username: str, password: str, testing: bool = False):
     try:
         dao = UserDAO()
         user = dao.get_user_by_username(username)
         uid = user[
             0]  # assuming that the uid is the first field in the row
         db_pass = json.loads(
             self.get_user_by_id(uid).get_data())['User']['upassword']
         if user and sha256_crypt.verify(password, db_pass):
             if not testing:
                 session['logged_in'] = True
                 session['uid'] = uid
             return True
         return False
     except Exception as e:
         print(e)
         if not testing:
             flash('Error on login')
         return False
예제 #9
0
 def check_login(self, json_input):
     if len(json_input
            ) != 2:  # check if there are sufficient elements in input
         return jsonify(Error="Malformed insert user request"), 400
     try:  # check parameters are valid
         uusername = json_input['uusername']
         upassword = json_input['upassword']
     except:
         return jsonify(Error="Unexpected attributes in login request"), 400
     try:
         if uusername and upassword:
             uid = LoginDAO().get_login_by_username_and_password(
                 uusername, upassword)
         else:
             return jsonify(Error="One or more attribute is empty"), 400
     except:
         return jsonify(Error="Login failed horribly."), 400
     # Finally returns an user dict of the inserted user.
     return jsonify(
         User=self.createUserDict(UserDAO().get_user_by_id(uid))), 200
예제 #10
0
class UserHandlerTestCase(unittest.TestCase):
    # unit tests for validating user operations
    def setUp(self):
        self.r1 = [
            1, 'Toilet Paper', 'this is NOT a description', 'home',
            'Requesting', 1
        ]
        self.r2 = [
            2, 'Hand Sanitizer', 'this MAY be a decription', 'Maya',
            'Provided', 3
        ]
        self.r3 = [
            3, 'Food', 'this is POSSIBLY a description', 'laCalle', 'Provided',
            1
        ]
        self.r4 = [
            4, 'send help pls', 'this is ME DOING a description', 'home',
            'Requesting', 3
        ]
        self.r5 = [
            5, 'beer', 'this is JUST a description', 'bar', 'Requesting', 4
        ]
        self.requests: list = [self.r1, self.r2, self.r3, self.r4, self.r5]
        self.user1 = [1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899']
        self.keys = [
            'rid', 'rtitle', 'rdescription', 'rlocation', 'rstatus', 'ruser'
        ]
        self.rh = RequestHandler()
        self.dao = RequestDAO()
        self.daoUSER = UserDAO()
        self.app = Flask(__name__)

    def test_create_request_dict(self):
        self.assertDictEqual(dict(zip(self.keys, self.r1)),
                             self.rh.create_request_dict(self.r1))
        self.assertDictEqual(dict(zip(self.keys, self.r2)),
                             self.rh.create_request_dict(self.r2))
        self.assertDictEqual(dict(zip(self.keys, self.r3)),
                             self.rh.create_request_dict(self.r3))
        self.assertDictEqual(dict(zip(self.keys, self.r4)),
                             self.rh.create_request_dict(self.r4))
        self.assertDictEqual(dict(zip(self.keys, self.r5)),
                             self.rh.create_request_dict(self.r5))

    def test_get_all_requests(self):
        with self.app.app_context():
            self.assertTrue(
                len(self.rh.get_all_requests()[0].json["Requests"]) > 1)

    def test_get_request_by_uid(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                request = self.rh.get_request_by_uid(uid)[0].json["Requests"]
                r[0] = rid
                self.assertIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))

    def test_get_requests_by_user_status(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                request = self.rh.get_requests_by_user_status(
                    rid=r[5], status=r[4])[0].json["Requests"]
                r[0] = rid
                self.assertIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))

    def test_insert_request(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                request = self.rh.get_all_requests()[0].json["Requests"]
                r[0] = rid
                self.assertIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))

    def test_delete_request_by_id(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                r[0] = rid
                self.rh.delete_request_by_id(r[0])
                request = self.rh.get_all_requests()[0].json["Requests"]
                self.assertNotIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))

    def test_get_request_by_location(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                request = self.rh.get_request_by_location(
                    r[3])[0].json["Requests"]
                r[0] = rid
                self.assertIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))

    def test_get_request_by_status(self):
        with self.app.app_context():
            uid = self.daoUSER.insert_user(self.user1[1], self.user1[2],
                                           self.user1[3], self.user1[4])
            for r in self.requests:
                r[5] = uid
                rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5])
                request = self.rh.get_request_by_status(
                    r[4])[0].json["Requests"]
                r[0] = rid
                self.assertIn(dict(zip(self.keys, r)), request)
            print("DELETED", self.daoUSER.delete_user_by_id(uid))
예제 #11
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