Ejemplo n.º 1
0
 def test_passwords_are_random(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     user_one = add_user('dutra', '*****@*****.**', 'greaterthaneight',
                         company.id)
     company_two = add_company('Kalkuli', '00.000.000/0000-00',
                               '*****@*****.**', 'kaliu', '789548546',
                               'ceilandia', 'df', '40028922')
     user_two = add_user('lucas', '*****@*****.**', 'greaterthaneight',
                         company_two.id)
     self.assertNotEqual(user_one.password, user_two.password)
Ejemplo n.º 2
0
 def test_encode_auth_token(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     user = add_user('dutra', '*****@*****.**', 'test', company.id)
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
Ejemplo n.º 3
0
 def test_user_registration(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     with self.client:
         response = self.client.post('/auth/register',
                                     data=json.dumps({
                                         'company_name': 'Kalkuli',
                                         'cnpj': '00.000.000/0000-00',
                                         'company_email':
                                         '*****@*****.**',
                                         'fantasy_name': 'Kaliu',
                                         'cep': '00-000/00',
                                         'city': 'Brasilia',
                                         'state': 'Distrito Federal',
                                         'company_phone': '61 98888888',
                                         'username': '******',
                                         'email': '*****@*****.**',
                                         'password': '******'
                                     }),
                                     content_type='application/json')
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'success')
         self.assertTrue(data['message'] == 'Successfully registered.')
         self.assertTrue(data['auth_token'])
         self.assertTrue(response.content_type == 'application/json')
         self.assertEqual(response.status_code, 201)
Ejemplo n.º 4
0
 def test_user_registration_duplicate_username(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     add_user('test', '*****@*****.**', 'test', company.id)
     with self.client:
         response = self.client.post(
             '/auth/register',
             data=json.dumps({
                 'company_name': 'Kalkuli',
                 'cnpj': '00.000.000/0000-00',
                 'company_email': '*****@*****.**',
                 'fantasy_name': 'Kaliu',
                 'cep': '00-000/00',
                 'city': 'Brasilia',
                 'state': 'Distrito Federal',
                 'company_phone': '61 98888888',
                 'username': '******',
                 'email': '[email protected]',
                 'password': '******'
             }),
             content_type='application/json',
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Sorry. That user already exists.', data['message'])
         self.assertIn('fail', data['status'])
Ejemplo n.º 5
0
def seeduserdb():
    company = add_company('Kalkuli', '00.000.000/0000-00', '*****@*****.**',
                          'kaliu', '789548546', 'ceilandia', 'df', '40028922')
    db.session.add(
        User(username='******',
             email='*****@*****.**',
             password='******',
             company_id=company.id))
    company_two = add_company('Kalkuli', '00.000.000/0000-00', '*****@*****.**',
                              'kaliu', '789548546', 'ceilandia', 'df',
                              '40028922')
    db.session.add(
        User(username='******',
             email='*****@*****.**',
             password='******',
             company_id=company_two.id))
    db.session.commit()
Ejemplo n.º 6
0
 def test_to_json(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     user = add_user('dutra', '*****@*****.**', 'greaterthaneight',
                     company.id)
     db.session.add(user)
     db.session.commit()
     self.assertTrue(isinstance(user.to_json(), dict))
Ejemplo n.º 7
0
 def test_add_user(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     user = add_user('dutra', '*****@*****.**', 'test', company.id)
     self.assertTrue(user.id)
     self.assertEqual(user.username, 'dutra')
     self.assertEqual(user.email, '*****@*****.**')
     self.assertTrue(user.active)
     self.assertTrue(user.password)
     self.assertEqual(user.company_id, company.id)
Ejemplo n.º 8
0
    def test_login_not_registered_user(self):
        company = add_company('Kalkuli', '00.000.000/0000-00',
                              '*****@*****.**', 'kaliu', '789548546',
                              'ceilandia', 'df', '40028922')
        user = add_user('test', '*****@*****.**', 'test', company.id)

        with self.client:
            resp_login = self.client.post('/auth/login',
                                          data=json.dumps({
                                              'email':
                                              '*****@*****.**',
                                              'password':
                                              '******'
                                          }),
                                          content_type='application/json')

            data = json.loads(resp_login.data.decode())

            self.assertIn('User does not exist.', data['message'])
            self.assertEqual(resp_login.status_code, 404)
Ejemplo n.º 9
0
 def test_invalid_logout_expired_token(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     add_user('test', '*****@*****.**', 'test', company.id)
     with self.client:
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps({
                                           'email': '*****@*****.**',
                                           'password': '******'
                                       }),
                                       content_type='application/json')
         time.sleep(4)
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             '/auth/logout', headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'fail')
         self.assertTrue(
             data['message'] == 'Signature expired. Please log in again.')
         self.assertEqual(response.status_code, 401)
Ejemplo n.º 10
0
 def test_valid_logout(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     add_user('test', '*****@*****.**', 'test', company.id)
     with self.client:
         # user login
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps({
                                           'email': '*****@*****.**',
                                           'password': '******'
                                       }),
                                       content_type='application/json')
         # valid token logout
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             '/auth/logout', headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'success')
         self.assertTrue(data['message'] == 'Successfully logged out.')
         self.assertEqual(response.status_code, 200)
Ejemplo n.º 11
0
 def test_invalid_status_inactive(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     add_user('test', '*****@*****.**', 'test', company.id)
     user = User.query.filter_by(email='*****@*****.**').first()
     user.active = False
     db.session.commit()
     with self.client:
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps({
                                           'email': '*****@*****.**',
                                           'password': '******'
                                       }),
                                       content_type='application/json')
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             '/auth/status', headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'fail')
         self.assertTrue(data['message'] == 'Provide a valid auth token.')
         self.assertEqual(response.status_code, 401)
Ejemplo n.º 12
0
 def test_user_status(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     add_user('test', '*****@*****.**', 'test', company.id)
     with self.client:
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps({
                                           'email': '*****@*****.**',
                                           'password': '******'
                                       }),
                                       content_type='application/json')
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             '/auth/status', headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'success')
         self.assertTrue(data['data'] is not None)
         self.assertTrue(data['data']['username'] == 'test')
         self.assertTrue(data['data']['email'] == '*****@*****.**')
         self.assertTrue(data['data']['active'] is True)
         self.assertEqual(response.status_code, 200)
Ejemplo n.º 13
0
 def test_registered_user_login(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     with self.client:
         add_user('test', '*****@*****.**', 'test', company.id)
         response = self.client.post('/auth/login',
                                     data=json.dumps({
                                         'email':
                                         '*****@*****.**',
                                         'password':
                                         '******',
                                         'company_id':
                                         company.id
                                     }),
                                     content_type='application/json')
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'success')
         self.assertTrue(data['message'] == 'Successfully logged in.')
         self.assertTrue(data['auth_token'])
         self.assertTrue(response.content_type == 'application/json')
         self.assertEqual(response.status_code, 200)
Ejemplo n.º 14
0
 def test_user_registration_invalid_json_keys_no_password(self):
     company = add_company('Kalkuli', '00.000.000/0000-00',
                           '*****@*****.**', 'kaliu', '789548546',
                           'ceilandia', 'df', '40028922')
     with self.client:
         response = self.client.post(
             '/auth/register',
             data=json.dumps({
                 'company_name': 'Kalkuli',
                 'cnpj': '00.000.000/0000-00',
                 'company_email': '*****@*****.**',
                 'fantasy_name': 'Kaliu',
                 'cep': '00-000/00',
                 'city': 'Brasilia',
                 'state': 'Distrito Federal',
                 'username': '******',
                 'email': '*****@*****.**'
             }),
             content_type='application/json',
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])