예제 #1
0
def registration():
    data = request.get_json()

    # Validate user
    validate_user = UserValidator(data)
    validation_methods = [
        validate_user.valid_email, validate_user.valid_name,
        validate_user.valid_phoneNumber, validate_user.validate_password,
        validate_user.matching_password
    ]

    for error in validation_methods:
        if error():
            return make_response(jsonify({
                "error": error(),
                "status": 422
            }), 422)

    # Register user
    user_data = {
        "first_name": data['first_name'],
        "last_name": data['last_name'],
        "othername": data['othername'],
        "email": data['email'],
        "phoneNumber": data['phoneNumber'],
        "username": data['username'],
        "password": data['password']
    }

    reg_user = User(user_data)
    if reg_user.save_user():
        return make_response(jsonify(reg_user.save_user()), 409)
    else:
        id = reg_user.fetch_user_id(user_data['username'])
        auth_token = reg_user.encode_auth_token(id[0])
        return make_response(
            jsonify({
                "status":
                201,
                "message":
                "{} registered successfully".format(data['email']),
                "username":
                data['username'],
                "auth_token":
                auth_token.decode('utf-8')
            }), 201)
예제 #2
0
class TestBaseModel(unittest.TestCase):

    def setUp(self):

        self.user_item = dict(
            first_name="David",
            last_name="Mwangi",
            othername="Dave",
            email="*****@*****.**",
            phoneNumber="0729710290",
            username="******",
            password="******",
        )

        self.user = User(self.user_item, os.getenv('TEST_DATABASE_URI'))
        self.user.base_model.database.create_tables()


    def test_save_user(self):
        """ Test that save user method works correctly """

        # add item
        self.user.save_user()
        user = self.user.fetch_specific_user('username', "username = '******'")
        self.assertTrue(user)


    def test_fetch_user_id(self):
        """ Test fetch user id method works correctly """

        self.user.save_user()
        self.assertTrue(self.user.fetch_user_id('dave'))
        self.assertFalse(self.user.fetch_user_id('mash'))


    def test_fetch_all_users(self):
        """ Test fetch all users method works correctly """

        users = self.user.fetch_all_users()

        # zero users
        self.assertFalse(users)

        # one user
        self.user.save_user()
        self.assertTrue(self.user.fetch_all_users())


    def test_fetch_specific_user(self):
        """ Test fetch specific user method works correctly """

        self.user.save_user()
        self.assertTrue(self.user.fetch_specific_user('id', "username = '******'"))


    def test_log_in_user(self):
        """ Test log in user method works correctly """

        self.user.save_user()
        user = {
            "email": self.user_item['email'],
            "password": self.user_item['password']
        }
        self.assertEqual(self.user.log_in_user(user), 1)

    
    def test_make_admin(self):
        """ Test make admin method works correctly """

        self.user.save_user()
        admin = self.user.fetch_specific_user('isAdmin', "username = '******'")[0]
        self.assertEqual(admin, False)
        # self.user.make_admin(1)
        # self.assertEqual(admin, True)
        # self.assertTrue(self.user.make_admin(1))
        

    def tearDown(self):
        """ This method destroys the test tables """

        self.user.base_model.database.drop_tables()