Beispiel #1
0
def route_login():
    '''This is the route for login.'''
    if request.method == 'POST':
        # getting the user information from the form and getting the information from the db
        input_dict = request.get_json(force=True)
        user_username = input_dict['user']
        user_password = input_dict['password']
        user = login(user_username, user_password)
        if user:
            # Generate our token
            auth_token = user.encode_auth_token()
            _log.debug(dir(auth_token))
            response = make_response(jsonify(user.to_dict()))
            response.set_cookie('authorization', auth_token.decode())
            return response, 200
        else:
            return {}, 401
    elif request.method == 'GET':
        auth_token = request.cookies.get('authorization')
        if auth_token:
            _log.debug(auth_token)
            _log.debug(User.decode_auth_token(auth_token))
            return jsonify(
                read_user_by_username(User.decode_auth_token(auth_token))), 200
        else:
            return {}, 401
    elif request.method == 'DELETE':
        empty = make_response({})
        empty.set_cookie('authorization', '')
        return empty, 204
    else:
        return '', 501
Beispiel #2
0
class UserTestSuite(unittest.TestCase):
    '''Test suite for User class'''
    user = None

    def setUp(self):
        self.user = User('username', 'password')

    def tearDown(self):
        self.user = None

    @classmethod
    def setUpClass(cls):
        cls.user = User()

    @classmethod
    def tearDownClass(cls):
        cls.user = None

    def test_get_id(self):
        '''Test for the get_id function.'''
        self.user = User("username", "password")
        self.assertEqual(-1, UserTestSuite.user.get_id())

    def test_get_username(self):
        '''Test for the get_username function.'''
        self.user = User("username", "password")
        self.assertEqual("username", self.user.get_username())

    def test_get_password(self):
        '''Test for the get_password function.'''
        self.user = User("username", "password")
        self.assertEqual("password", self.user.get_password())

    def test_set_id(self):
        '''Test for the set_id function.'''
        UserTestSuite.user.set_id(10)
        self.assertEqual(10, UserTestSuite.user._id)

    def test_set_username(self):
        '''Test for the set_username funciton.'''
        UserTestSuite.user.set_username('hello')
        self.assertEqual('hello', UserTestSuite.user.username)

    def test_login(self):
        '''Test for the login function.'''
        self.user = User("username", "password")
        self.assertTrue(self.user.login("username", "password"))
        self.assertFalse(UserTestSuite.user.login("notusername",
                                                  "notpassword"))

    def test_to_dict(self):
        '''Test for the to_dict function.'''
        self.user = User('username', 'password')
        self.assertIs(type(UserTestSuite.user.to_dict()), dict)
Beispiel #3
0
 def test_to_dict(self):
     '''Test for the to_dict function.'''
     self.user = User('username', 'password')
     self.assertIs(type(UserTestSuite.user.to_dict()), dict)
Beispiel #4
0
 def test_login(self):
     '''Test for the login function.'''
     self.user = User("username", "password")
     self.assertTrue(self.user.login("username", "password"))
     self.assertFalse(UserTestSuite.user.login("notusername",
                                               "notpassword"))
Beispiel #5
0
 def test_get_password(self):
     '''Test for the get_password function.'''
     self.user = User("username", "password")
     self.assertEqual("password", self.user.get_password())
Beispiel #6
0
 def test_get_id(self):
     '''Test for the get_id function.'''
     self.user = User("username", "password")
     self.assertEqual(-1, UserTestSuite.user.get_id())
Beispiel #7
0
 def setUpClass(cls):
     cls.user = User()
Beispiel #8
0
 def setUp(self):
     self.user = User('username', 'password')