예제 #1
0
 def test_add_user(self):
     """Ensure a new user can be added to the database."""
     add_admin("test", "*****@*****.**", "testpass123")
     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.post(
             "/users",
             data=json.dumps({
                 "username": "******",
                 "email": "*****@*****.**",
                 "password": "******",
             }),
             content_type="application/json",
             headers={"Authorization": f"Bearer {token}"},
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn("[email protected] was added!", data["message"])
         self.assertIn("success", data["status"])
예제 #2
0
 def test_add_user_invalid_json_keys_no_password(self):
     """Garante que um erro é jogado se o JSON não tiver um password"""
     add_admin('test', '*****@*****.**', 'test')
     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.post(
             '/users',
             data=json.dumps(dict(
                 username='******',
                 email='*****@*****.**')),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
예제 #3
0
 def test_add_user_invalid_json_keys_no_password(self):
     """
     Ensure error is thrown if the JSON object
     does not have a password key.
     """
     add_admin('test', '*****@*****.**', 'test')
     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.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload', data['message'])
         self.assertIn('fail', data['status'])
예제 #4
0
 def test_add_user_inactive(self):
     add_admin('test', '*****@*****.**', 'test')
     # update user
     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.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             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)
예제 #5
0
 def test_add_user_invalid_json_keys_no_password(self):
     """
     Ensure that error is thrown if the json object
     does not have a password key
     """
     add_admin("test", "*****@*****.**", "test")
     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.post(
             "/users",
             data=json.dumps(
                 dict(username="******",
                      email="*****@*****.**")),
             content_type="application/json",
             headers={"Authorization": f"Bearer {token}"},
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn("Invalid payload.", data["message"])
         self.assertIn("fail", data["status"])
 def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email already exists."""
     add_admin('test', '*****@*****.**', 'test')
     resp_login = self.login_user('*****@*****.**', 'test')
     token = json.loads(resp_login.data.decode())['auth_token']
     with self.client:
         self.client.post('/users',
                          data=json.dumps({
                              'username': '******',
                              'email': '*****@*****.**',
                              'password': '******'
                          }),
                          content_type='application/json',
                          headers={'Authorization': f'Bearer {token}'})
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Sorry. That email already exists.', data['message'])
         self.assertIn('fail', data['status'])
예제 #7
0
 def test_add_user_invalid_json_keys(self):
     """
     Ensure error is thrown if the JSON object does not have a username key.
     """
     add_admin("test", "*****@*****.**", "testpass123")
     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.post(
             "/users",
             data=json.dumps(
                 dict(email="*****@*****.**", password="******")),
             content_type="application/json",
             headers={"Authorization": f"Bearer {token}"},
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn("Invalid payload.", data["message"])
         self.assertIn("fail", data["status"])
예제 #8
0
 def test_add_user(self):
     """Ensure a new user can be added to the database."""
     # Add a test user, that we will login in with
     add_admin('test', '*****@*****.**', 'test')
     with self.client:
         # Login with the test user
         resp_login = self.client.post(
             '/auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         # Get the token.
         # User needs to be authenticated to make a post request to /users
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             # Add the token to the request, for authentication
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn('[email protected] was added!', data['message'])
         self.assertIn('success', data['status'])
예제 #9
0
 def test_add_user_duplicate_email(self):
     add_admin('test', '*****@*****.**', 'password')
     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']
     self.client.post(
         '/users',
         data=json.dumps({
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         }),
         content_type='application/json',
         headers={'Authorization': f'Bearer {token}'})
     response = self.client.post(
         '/users',
         data=json.dumps({
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         }),
         content_type='application/json',
         headers={'Authorization': f'Bearer {token}'})
     data = json.loads(response.data.decode())
     self.assertEqual(response.status_code, 400)
     self.assertIn(
         'Sorry. That email already exists.', data['message'])
     self.assertIn('fail', data['status'])
예제 #10
0
 def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email already exists."""
     add_admin('test', '*****@*****.**', 'test')
     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']
         self.client.post('/users',
                          data=json.dumps({
                              'username': '******',
                              'email': '*****@*****.**',
                              'password': '******',
                          }),
                          content_type='application/json',
                          headers={'Authorization': f'Bearer {token}'})
         token_two = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******',
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token_two}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Sorry. That email already exists.', data['message'])
         self.assertIn('fail', data['status'])
예제 #11
0
 def test_add_user_invalid_json_keys(self):
     """
     Ensure error is thrown if the JSON object does not have
     a username key.
     """
     # Add a test user, that we will login in with
     add_admin('test', '*****@*****.**', 'test')
     with self.client:
         # Login with the test user
         resp_login = self.client.post(
             '/auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
         )
         # Get the token.
         # You need to be authenticated to make a post request to /users
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
                 }),
             content_type='application/json',
             # Add the token to the request, for authentication
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
예제 #12
0
 def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email is already existing."""
     add_admin('test', '*****@*****.**', 'jianxin1234')
     response = self.client.post('/auth/login',
                                 json={
                                     'email': '*****@*****.**',
                                     'password': '******'
                                 })
     token = response.get_json().get('auth_token')
     self.client.post('/users',
                      json={
                          'username': '******',
                          'email': '*****@*****.**',
                          'password': '******'
                      },
                      headers={'Authorization': f'Bearer {token}'})
     response = self.client.post(
         '/users',
         json={
             'username': '******',
             'email': '*****@*****.**',
             'password': '******'
         },
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(response.status_code, 400)
     self.assertIn('Sorry, the email is already existing.', data['message'])
     self.assertIn('fail', data['status'])
예제 #13
0
 def test_add_user(self):
     """Ensure a new user can be added to the database."""
     with self.client:
         add_admin('test', '*****@*****.**', 'test')
         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.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'}
         )
     data = json.loads(response.data.decode())
     self.assertEqual(response.status_code, 201)
     self.assertIn('[email protected] was added', data['message'])
     self.assertIn('success', data['status'])
예제 #14
0
 def test_add_user_invalid_json_keys(self):
     """Ensure error is thrown if JSON object does not have a username
     key."""
     with self.client:
         add_admin('test', '*****@*****.**', 'test')
         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.post(
             '/users',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
예제 #15
0
 def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email already exists."""
     add_admin("test", "*****@*****.**", "testpass123")
     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.post(
             "/users",
             data=json.dumps({
                 "username": "******",
                 "email": "*****@*****.**",
                 "password": "******",
             }),
             content_type="application/json",
             headers={"Authorization": f"Bearer {token}"},
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn("Sorry. That email already exists.", data["message"])
         self.assertIn("fail", data["status"])
예제 #16
0
 def test_add_user(self):
     """
     Garante que um novo usuário possa ser adicionado ao banco de dados.
     """
     add_admin('test', '*****@*****.**', 'test')
     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.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn('[email protected] was added!', data['message'])
         self.assertIn('success', data['status'])
 def test_add_user_invalid_json(self):
     """Ensure error is thrown if the JSON object is empty"""
     add_admin()
     with self.client:
         resp_login = log_in(self.client)
         token = json.loads(resp_login.data.decode())["auth_token"]
         response = add_users(self.client, token, {})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertEqual(data["status"], "fail")
         self.assertEqual(data["message"], "Invalid payload")
 def test_add_user(self):
     """Ensure a new user can be added to the database"""
     add_admin()
     with self.client:
         resp_login = log_in(self.client)
         token = json.loads(resp_login.data.decode())["auth_token"]
         response = add_users(self.client, token)
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertEqual(data["status"], "success")
         self.assertEqual(data["message"], "[email protected] was added!")
예제 #19
0
    def get_valid_token(self):
        add_admin('test', '*****@*****.**', 'test')
        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']

            return token
예제 #20
0
 def test_add_user_invalid_json_empty(self):
     """Ensure error is thrown if the json object is empty."""
     add_admin('test', '*****@*****.**', 'jianxin1234')
     response = self.client.post('/auth/login',
                                 json={
                                     'email': '*****@*****.**',
                                     'password': '******'
                                 })
     token = response.get_json().get('auth_token')
     response = self.client.post(
         '/users', json={}, headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(response.status_code, 400)
     self.assertIn('Invalid payload', data['message'])
     self.assertIn('fail', data['status'])
 def test_add_user_invalid_json(self):
     """Ensure error is thrown if the JSON object is empty."""
     add_admin('test', '*****@*****.**', 'test')
     resp_login = self.login_user('*****@*****.**', 'test')
     token = json.loads(resp_login.data.decode())['auth_token']
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps({}),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
 def test_add_user_empty_email(self):
     """
     Ensure error is thrown if the JSON object does not have a email key
     """
     add_admin()
     with self.client:
         resp_login = log_in(self.client)
         token = json.loads(resp_login.data.decode())["auth_token"]
         response = add_users(self.client, token, {
             "username": "******",
             "password": "******"
         })
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertEqual(data["status"], "fail")
         self.assertEqual(data["message"], "Invalid payload")
 def test_add_user_duplicate_email(self):
     """Ensure error is thrown if the email already exists"""
     add_admin()
     with self.client:
         resp_login = log_in(self.client)
         token = json.loads(resp_login.data.decode())["auth_token"]
         response = add_users(self.client, token, {
             "username": "******",
             "email": "*****@*****.**",
             "password": "******"
         })
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertEqual(data["status"], "fail")
         self.assertEqual(data["message"],
                          "Sorry. That email already exists")
예제 #24
0
    def test_all_users(self):
        """Ensure get all users behaves correctly."""
        add_admin('jianxin', '*****@*****.**', 'password1234')
        add_user('changqing', '*****@*****.**', 'password1234')
        response = self.client.get('/users')
        data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(data['data']['users']), 2)
        self.assertIn('jianxin', data['data']['users'][0]['username'])
        self.assertIn('*****@*****.**', data['data']['users'][0]['email'])
        self.assertTrue(data['data']['users'][0]['admin'])

        self.assertIn('changqing', data['data']['users'][1]['username'])
        self.assertIn('*****@*****.**', data['data']['users'][1]['email'])
        self.assertIn('success', data['status'])
        self.assertFalse(data['data']['users'][1]['admin'])
예제 #25
0
    def test_add_users_invalid_json_keys(self):
        """ Error is thrown if invalid json keys """
        user = add_admin('test', '*****@*****.**', 'test')
        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.post(
                '/flowers',
                data=json.dumps({
                    'email': '*****@*****.**',
                    # 'password': '******' # must be non-empty - Weird
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 400)
            self.assertIn('Invalid payload', data['message'])
예제 #26
0
 def test_add_user(self):
     """Ensure a new user can be added to the database."""
     user = add_admin(user_test()['username'],
                      user_test()['email'],
                      user_test()['password'], True)
     with self.client:
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps(user_test()),
                                       content_type='application/json')
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.post(
             '/users',
             data=json.dumps(user_json_login()),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn('[email protected] was added!', data['message'])
         self.assertIn('success', data['status'])
         self.assertTrue(user.id)
         self.assertEqual(user.username, 'test')
         self.assertEqual(user.email, user_test()['email'])
         self.assertTrue(user.password)
         self.assertTrue(user.active)
         self.assertTrue(user.admin)
예제 #27
0
    def test_add_users(self):
        # user = add_user('test', '*****@*****.**', 'test')
        # user = User.query.filter_by(email='*****@*****.**').first()
        # user.admin=True
        # db.session.commit()
        user = add_admin('test', '*****@*****.**', 'test')

        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']
            # import pdb; pdb.set_trace()
            response = self.client.post(
                '/flowers',
                data=json.dumps({
                    'username': '******',
                    'email': '*****@*****.**',
                    'password': '******'
                }),
                content_type='application/json',
                headers={'Authorization': f'Bearer {token}'})
            data = json.loads(response.data.decode())
            self.assertEqual(response.status_code, 201)
            self.assertIn('[email protected] was added', data['message'])
            self.assertTrue(user.admin)
 def test_add_user_duplicate_email(self):
     user = add_admin('justatest', '*****@*****.**', 'greaterthaneight')
     duplicate_user = User(username='******',
                           email='*****@*****.**',
                           password='******')
     db.session.add(duplicate_user)
     with self.assertRaises(IntegrityError):
         db.session.commit()
예제 #29
0
 def test_add_user_invalid_json(self):
     """Ensure error is thrown if the JSON object is empty."""
     add_admin(user_test()['username'],
               user_test()['email'],
               user_test()['password'], True)
     with self.client:
         resp_login = self.client.post('/auth/login',
                                       data=json.dumps(user_test()),
                                       content_type='application/json')
         token = json.loads(resp_login.data.decode())['auth_token']
         rv = self.client.post('/users',
                               data=json.dumps({}),
                               content_type='application/json',
                               headers={'Authorization': f'Bearer {token}'})
         data = json.loads(rv.data.decode())
         self.assertEqual(rv.status_code, 400)
         self.assertIn('Invalid payload.', data['message'])
         self.assertIn('fail', data['status'])
 def test_add_user(self):
     """Ensure a new user can be added to the database"""
     add_admin('test', '*****@*****.**', 'test')
     resp_login = self.login_user('*****@*****.**', 'test')
     token = json.loads(resp_login.data.decode())['auth_token']
     with self.client:
         response = self.client.post(
             '/users',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json',
             headers={'Authorization': f'Bearer {token}'})
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 201)
         self.assertIn('[email protected] was added!', data['message'])
         self.assertIn('success', data['status'])