def test_add_points(self): test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_user) self.db.session.commit() test_user.add_points(5) self.assertEqual(test_user.points, 5)
def test_password_hashing(self): test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_user) self.db.session.commit() self.assertTrue( bcrypt.check_password_hash(test_user.password, 'insecure'))
def post(self): post_data = request.get_json() log.debug('Registration Request: {}'.format(json.dumps(post_data))) # check if user already exists user = User.query.filter_by(email=post_data.get('email')).first() if not user: try: # Attempt to create user user = User( email=post_data.get('email'), password=post_data.get('password') ) db.session.add(user) db.session.commit() responseObject = { 'status': 'success', 'message': 'Successfully registered.' } log.debug('User Created: {}-{}'.format(user.id, user.email)) return make_response(jsonify(responseObject), 201) except Exception as e: # User creation failed log.error('User creation failed. Error: {}. Params: {}'.format(e, json.dumps(post_data))) responseObject = { 'status': 'fail', 'message': 'Some error occurred. Please try again.' } return make_response(jsonify(responseObject), 401) else: responseObject = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return make_response(jsonify(responseObject), 202)
def test_query(self): test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_user) self.db.session.commit() users = User.query.all() self.assertTrue(users)
def test_add_user_to_org(self): test_org = Organization(name="The Zesty Testy Inc.") test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_org) self.db.session.add(test_user) self.db.session.commit() test_org.add_user(test_user) self.assertTrue(test_org.users)
def test_remove_user_from_org(self): test_org = Organization(name="The Zesty Testy Inc.") test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_org) self.db.session.add(test_user) self.db.session.commit() test_org.add_user(test_user) test_org.remove_user(test_org.users[0]) self.assertFalse(test_org.users)
def test_get_track(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() track_id = track.id # Test res = self.app.get('/tracks/' + str(track_id)) self.assertTrue(res.status_code == 200) res_json = json.loads(res.data.decode('utf-8').replace("'", "\"")) self.assertTrue(res_json['data']['name'] == 'Test Track')
def test_delete_track(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() track_id = track.id # Test res = self.app.delete('/tracks/' + str(track_id)) self.assertTrue(res.status_code == 202) deleted_track = Track.query.get(track_id) self.assertTrue(deleted_track == None)
def test_delete_goal(self): user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() goal = Goal(name="Test Goal", track_id=track.id) self.db.session.add(goal) self.db.session.commit() res = self.app.delete('/goals/' + str(goal.id)) self.assertTrue(res.status_code == 200) deleted_goal = Goal.query.get(goal.id) self.assertTrue(deleted_goal == None)
def test_create_track(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() # Test res = self.app.post('/tracks', data=json.dumps({ 'name': 'Test Track', 'user_id': user.id }), content_type='application/json') self.assertTrue(res.status_code == 201) track = Track.query.get(1) self.assertTrue(track.name == 'Test Track')
def test_get_tracks(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track1 = Track(name="Test Track 1", user_id=user.id) self.db.session.add(track1) track2 = Track(name="Test Track 2", user_id=user.id) self.db.session.add(track2) self.db.session.commit() # Test res = self.app.get('/tracks') self.assertTrue(res.status_code == 200) res_json = json.loads(res.data.decode('utf-8').replace("'", "\"")) self.assertTrue(len(res_json['data']) == 2) self.assertTrue([track['name'] for track in res_json['data']] == ['Test Track 1', 'Test Track 2'])
def test_get_goals(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() goal1 = Goal(name="Test Goal 1", track_id=track.id) self.db.session.add(goal1) goal2 = Goal(name="Test Goal 2", track_id=track.id) self.db.session.add(goal2) self.db.session.commit() res = self.app.get('/goals') self.assertTrue(res.status_code == 200) res_json = json.loads(res.data.decode('utf-8').replace("'", "\"")) self.assertTrue(len(res_json['data']) == 2)
def test_create_goal(self): user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() res = self.app.post('/goals', data=json.dumps({ 'name': 'Test Goal', 'track_id': track.id }), content_type='application/json') self.assertTrue(res.status_code == 201) goal = Goal.query.get(1) self.assertTrue(goal.name == 'Test Goal')
def test_update_goal(self): user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() goal = Goal(name="Test Goal", track_id=track.id) self.db.session.add(goal) self.db.session.commit() goal_id = goal.id res = self.app.put('/goals/' + str(goal.id), data=json.dumps({'name': 'Changed Goal'}), content_type='application/json') self.assertTrue(res.status_code == 200) changed_goal = Goal.query.get(goal_id) self.assertTrue(changed_goal.name == 'Changed Goal')
def test_update_track(self): # Create data... user = User(email="*****@*****.**", password="******") self.db.session.add(user) self.db.session.commit() track = Track(name="Test Track", user_id=user.id) self.db.session.add(track) self.db.session.commit() track_id = track.id # Test res = self.app.put('/tracks/' + str(track_id), data=json.dumps({ 'name': 'Changed', 'archived': 'true' }), content_type='application/json') self.assertTrue(res.status_code == 200) changed_track = Track.query.get(track_id) self.assertTrue(changed_track.name == 'Changed') self.assertTrue(changed_track.archived == True)
def create_test_user(self, email, password): user = User(email=email, password=password) self.db.session.add(user) self.db.session.commit() return user.id
def test_repr(self): test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_user) self.db.session.commit() self.assertEqual(repr(test_user), '<User [email protected]>')
def test_create(self): test_user = User(email="*****@*****.**", password="******") self.db.session.add(test_user) self.db.session.commit() self.assertTrue(True)