def test_delete(self): # Requires login: res = self.test_client.post("/goals/delete/", data={'id': 0}) setup.assertRequiresLogin(self, res) self.login() ## Validates form: res = self.test_client.post("/goals/delete/", data={'foo': 'bar'}) setup.assertInvalid(self, res, "id") ## works at all new_goal = self.create_test_goal() res = self.test_client.post("/goals/delete/", data={'id': new_goal['id']}) setup.assertOk(self, res, 204) ## 404s res = self.test_client.post("/goals/delete/", data={'id': new_goal['id']}) setup.assert404(self, res) ## Can't someone *else*'s goal. new_goal = self.create_test_goal() self.logout() self.login_other_user() res = self.test_client.post("/goals/delete/", data={'id': new_goal['id']}) setup.assertInvalidCredentials(self, res)
def test_register(self): tc = self.test_client invalid_user = {} for x, y in self.new_user.iteritems(): if x != 'password': invalid_user[x] = y res = tc.post('/register/', data=invalid_user) setup.assertInvalid(self, res, 'password') res = tc.post("/register/", data=self.new_user) setup.assertOk(self, res, 201) data = json.loads(res.data) self.assertIn('id', data) for field, value in self.new_user.iteritems(): if (field == 'password'): self.assertNotIn(field, data) else: self.assertEqual(value, data[field]) ## you should get a well-formatted error if the user already exists. self.new_user['password'] = "******" res = tc.post("/register/", data=self.new_user) self.assertEqual(res.status_code, 409) data = json.loads(res.data) self.assertIn('detail', data) self.assertIn('[email protected] already exists', data['detail']) self.new_user['password'] = "******"
def test_update(self): tc = self.test_client res = tc.post('/register/', data=self.new_user) setup.assertOk(self, res, 201) user = json.loads(res.data) post_data = {"password": self.new_user['password'], "first_name": "new-first-name"} res = tc.post('/users/update/', data=post_data) setup.assertInvalid(self, res, 'email') post_data['email'] = "not-real-email" res = tc.post('/users/update/', data=post_data) setup.assertInvalidCredentials(self, res) post_data['email'] = self.new_user['email'] post_data['password'] = '******' res = tc.post('/users/update/', data=post_data) setup.assertInvalidCredentials(self, res) post_data['password'] = self.new_user['password'] res = tc.post('/users/update/', data=post_data) setup.assertOk(self, res) res = tc.post('/login/', data={'email': self.new_user['email'], 'password': self.new_user['password']}) setup.assertOk(self, res, 201) res = tc.get('/users/me/') setup.assertOk(self, res) updated_user = json.loads(res.data) for key, value in user.iteritems(): if (key == 'first_name'): self.assertEqual(updated_user[key], 'new-first-name') else: self.assertEqual(updated_user[key], user[key])
def test_get_index(self): tc = self.test_client res = tc.get('/users/index/') setup.assertRequiresLogin(self, res) setup.create_test_users() res = tc.post('/login/', data={'email': "*****@*****.**", 'password': self.new_user['password']}) setup.assertOk(self, res, 201) res = tc.get('/users/index/') setup.assertOk(self, res) data = json.loads(res.data) self.assertIn('users', data) data = data['users'] self.assertEqual(len(data), 20) # default is 20 for x in range(ord('a'), ord('a') + 19): self.assertEqual(data[x - ord('a')]['email'], 'test_' + chr(x) + '@example.com') ## Should sort by email res = tc.get('/users/index/?count=3&offset=3') setup.assertOk(self, res) data = json.loads(res.data) self.assertIn('users', data) data = data['users'] self.assertEqual(len(data), 3) for x in range(ord('a'), ord('a') + 2): self.assertEqual(data[x - ord('a')]['email'], 'test_' + chr(x + 3) + '@example.com') ## Should still sort by email res = tc.get('/users/index/?count=3&offset=3&sort=last_name&sort_order=desc') setup.assertOk(self, res) data = json.loads(res.data) self.assertIn('users', data) data = data['users'] self.assertEqual(len(data), 3) for x in range(ord('a'), ord('a') + 2): self.assertEqual(data[x - ord('a')]['email'], 'test_' + chr(2 * ord('a') + 25 - x - 3) + '@example.com') res = tc.get('/users/index/?count=3&offset=3&sort=not_a_real_field&sort_order=desc') setup.assertInvalid(self, res, 'sort') res = tc.get('/users/index/?count=3&offset=3&sort=last_name&sort_order=pineapple') setup.assertInvalid(self, res, 'sort_order')
def test_update_password(self): tc = self.test_client res = tc.post("/register/", data=self.new_user) setup.assertOk(self, res, 201) ## Shoudl fail if missing a key res = tc.post('/users/update-password/', data={"email": self.new_user['email'], "new_password": "******"}) setup.assertInvalid(self, res, 'old_password') ## should fail for non-existent emails res = tc.post('/users/update-password/', data={"email": "notreal", "old_password": "******", "new_password": "******"}) setup.assertInvalidCredentials(self, res) res = tc.post('/users/update-password/', data={"email": self.new_user['email'], "old_password": "******", "new_password": "******"}) setup.assertInvalidCredentials(self, res) res = tc.post('/users/update-password/', data={"email": self.new_user['email'], "old_password": self.new_user['password'], "new_password": "******"}) setup.assertOk(self, res) res = tc.post("/login/", data={"email": self.new_user['email'], "password": "******"}) setup.assertOk(self, res, 201)
def test_create(self): # Requires login: res = self.test_client.post("/goals/create/", data=self.new_goal) setup.assertRequiresLogin(self, res) self.login() ## Test that it rejects bad inputs bad_goal = self.new_goal.copy() bad_goal['name'] = None res = self.test_client.post("/goals/create/", data=bad_goal) setup.assertInvalid(self, res, "name") bad_goal['name'] = self.new_goal['name'] bad_goal['prompt'] = "" res = self.test_client.post("/goals/create/", data=bad_goal) setup.assertBadData(self, res, "Prompt must be between 0 and 255 characters") ## Test that it actually registers at all :-) res = self.test_client.post("/goals/create/", data=self.new_goal) setup.assertOk(self, res, 201) data = json.loads(res.data) self.assertIn('id', data) goal = Goal.pull_by_id(data['id']) self.assertIsNotNone(goal) for key, val in self.new_goal.iteritems(): self.assertEqual(val, data[key]) if (key == 'frequency' or key == 'check_in_frequency'): self.assertEqual(val, Frequency.get_name_by_id(getattr(goal, key))) else: self.assertEqual(val, getattr(goal, key)) ## Shouldn't be able to register something that already exists res = self.test_client.post("/goals/create/", data=self.new_goal) self.assertEqual(res.status_code, 409) data = json.loads(res.data) self.assertIn('detail', data) self.assertIn('Test goal already exists', data['detail'])
def test_login(self): tc = self.test_client res = tc.post('/register/', data=self.new_user) res = tc.post('/login/', data={"email": self.new_user['email']}) setup.assertInvalid(self, res, 'password') res = tc.post('/login/', data={"email": "notreal", "password": self.new_user['password']}) data = json.loads(res.data) self.assertIn('detail', data) self.assertIn('notreal is not a registered', data['detail']) self.assertEqual(res.status_code, 401) res = tc.post('/login/', data={"email": self.new_user['email'], "password": "******"}) data = json.loads(res.data) self.assertIn('detail', data) self.assertIn('Incorrect password', data['detail']) self.assertEqual(res.status_code, 401) res = tc.post('/login/', data={"email": self.new_user['email'], "password": self.new_user['password']}) setup.assertOk(self, res, 201)
def test_destroy(self): user = User(self.new_user['email'], self.new_user['password'], self.new_user['first_name'], self.new_user['last_name']) user.persist() self.assertTrue(user.exists()) ## I don't understand why but this is necessary or else line 45 fails tc = self.test_client res = tc.post("/users/delete/", data={"email": "fake email"}) setup.assertInvalid(self, res, 'password') res = tc.post("/users/delete/", data={"email": "fake email", "password": "******"}) data = json.loads(res.data) setup.assertInvalidCredentials(self, res) self.assertTrue(user.exists()) tc = self.test_client res = tc.post("/users/delete/", data={"email": user.email, "password": "******"}) data = json.loads(res.data) setup.assertInvalidCredentials(self, res) self.assertTrue(user.exists()) res = tc.post("/users/delete/", data={"email": user.email, "password": "******"}) setup.assertOk(self, res, 204) self.assertFalse(user.exists())
def test_update_email(self): tc = self.test_client post_data = {"old_email": self.new_user['email'], "new_email": "*****@*****.**", "password": self.new_user['password']} res = tc.post('/users/update-email/', data=post_data) setup.assertInvalidCredentials(self, res) res = tc.post('/register/', data=self.new_user) setup.assertOk(self, res, 201) post_data['password'] = '******' res = tc.post('/users/update-email/', data=post_data) setup.assertInvalidCredentials(self, res) post_data['password'] = None res = tc.post('/users/update-email/', data=post_data) setup.assertInvalid(self, res, 'password') post_data['password'] = self.new_user['password'] res = tc.post('/users/update-email/', data=post_data) setup.assertOk(self, res) res = tc.post('/login/', data={"email": post_data['new_email'], "password": post_data['password']}) setup.assertOk(self, res, 201)
def test_update(self): ## Requires login res = self.test_client.post("/goals/update/", data={'id': 0}) setup.assertRequiresLogin(self, res) ## Validates form self.login() res = self.test_client.post("/goals/update/", data={'favorite_muppet': 'kermit'}) setup.assertInvalid(self, res, 'id') ## 404's res = self.test_client.post("/goals/update/", data={'id': 0, 'name': 'test goal 2.0'}) setup.assert404(self, res) ## Actually works goal = self.create_test_goal() data = {'id': goal['id']} data['name'] = "Test Goal 2.0" data['prompt'] = "Guess how many eggs I had for breakfast today!" data['frequency'] = "monthly" data['target'] = 100 data['input_type'] = "binary" data['active'] = False data['public'] = True data['check_in_frequency'] = "daily" res = self.test_client.post("/goals/update/", data=data) setup.assertOk(self, res, 200) updated_goal = Goal.pull_by_id(goal['id']) for key, val in data.iteritems(): if (key == 'frequency' or key == 'check_in_frequency'): self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val) else: self.assertEqual(getattr(updated_goal, key), val) ## If any part is invalid, no part should go through bad_data = {'id': goal['id']} bad_data['name'] = "Test Goal 3.0" bad_data['prompt'] = "How many jars of sand did you collect?" bad_data['frequency'] = "daily" bad_data['target'] = 1000 bad_data['input_type'] = "numeric" bad_data['active'] = True bad_data['public'] = "banana" bad_data['check_in_frequency'] = "daily" res = self.test_client.post("/goals/update/", data=bad_data) setup.assertBadData(self, res, "Public must be a boolean") updated_goal = Goal.pull_by_id(goal['id']) for key, val in data.iteritems(): if (key == 'frequency' or key == 'check_in_frequency'): self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val) else: self.assertEqual(getattr(updated_goal, key), val) ## Can't update someone else's goal self.logout() self.login_other_user() res = self.test_client.post("/goals/update/", data=bad_data) ## using bad_data here ensures we don't update setup.assertInvalidCredentials(self, res)
def test_update(self): ## Requires login res = self.test_client.post("/goals/update/", data={'id': 0}) setup.assertRequiresLogin(self, res) ## Validates form self.login() res = self.test_client.post("/goals/update/", data={'favorite_muppet': 'kermit'}) setup.assertInvalid(self, res, 'id') ## 404's res = self.test_client.post("/goals/update/", data={ 'id': 0, 'name': 'test goal 2.0' }) setup.assert404(self, res) ## Actually works goal = self.create_test_goal() data = {'id': goal['id']} data['name'] = "Test Goal 2.0" data['prompt'] = "Guess how many eggs I had for breakfast today!" data['frequency'] = "monthly" data['target'] = 100 data['input_type'] = "binary" data['active'] = False data['public'] = True data['check_in_frequency'] = "daily" res = self.test_client.post("/goals/update/", data=data) setup.assertOk(self, res, 200) updated_goal = Goal.pull_by_id(goal['id']) for key, val in data.iteritems(): if (key == 'frequency' or key == 'check_in_frequency'): self.assertEqual( Frequency.get_name_by_id(getattr(updated_goal, key)), val) else: self.assertEqual(getattr(updated_goal, key), val) ## If any part is invalid, no part should go through bad_data = {'id': goal['id']} bad_data['name'] = "Test Goal 3.0" bad_data['prompt'] = "How many jars of sand did you collect?" bad_data['frequency'] = "daily" bad_data['target'] = 1000 bad_data['input_type'] = "numeric" bad_data['active'] = True bad_data['public'] = "banana" bad_data['check_in_frequency'] = "daily" res = self.test_client.post("/goals/update/", data=bad_data) setup.assertBadData(self, res, "Public must be a boolean") updated_goal = Goal.pull_by_id(goal['id']) for key, val in data.iteritems(): if (key == 'frequency' or key == 'check_in_frequency'): self.assertEqual( Frequency.get_name_by_id(getattr(updated_goal, key)), val) else: self.assertEqual(getattr(updated_goal, key), val) ## Can't update someone else's goal self.logout() self.login_other_user() res = self.test_client.post( "/goals/update/", data=bad_data) ## using bad_data here ensures we don't update setup.assertInvalidCredentials(self, res)
def test_check_in(self): tc = self.test_client # Requires Login: res = tc.post("/goals/1/check-ins/", data={"value": 1}) setup.assertRequiresLogin(self, res) ## Requires "value" self.login() res = tc.post("/goals/1/check-ins/", data={"not-value": 1}) setup.assertInvalid(self, res, "value") ## Must be a goal that exists res = tc.post("/goals/0/check-ins/", data={"value": 1}) setup.assert404(self, res) ## User must own the goal in question numeric_goal = self.create_test_numeric_goal() numeric_goal_id = str(numeric_goal.get_id( )) ## You have to do this before you log out, for some reason? binary_goal = self.create_test_binary_goal() binary_goal_id = str(binary_goal.get_id()) self.logout() self.login_other_user() res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1}) setup.assertInvalidCredentials(self, res) ## Check-in must conform (true / false vs. numeric) self.logout() self.login() res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": "true"}) setup.assertBadData(self, res, "Value must be numeric") res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": 10}) setup.assertBadData(self, res, "Value must be a boolean") ## Check-in is returned with 201 if no timeframe is given, returning current timeframe (both numeric & binary) res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1}) setup.assertOk(self, res, 201) data = json.loads(res.data) self.assertIn("id", data) self.assertEqual(str(data['goal']), numeric_goal_id) self.assertEqual(data['value'], 1) self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict()) self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data) res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": True}) setup.assertOk(self, res, 201) data = json.loads(res.data) self.assertIn("id", data) self.assertEqual(str(data['goal']), binary_goal_id) self.assertEqual(data['value'], 1) self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict()) self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data) ## An updated check-in is returned with 200 if no timeframe is given, returning current timeframe res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 3}) setup.assertOk(self, res, 200) data = json.loads(res.data) self.assertIn("id", data) self.assertEqual(str(data['goal']), numeric_goal_id) self.assertEqual(data['value'], 3) self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict()) self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data) ## Check-in is returned with 201 if timeframe ID is given, returning correct timeframe tf = Timeframe.get_timeframe("daily", datetime.datetime(2016, 1, 1)) res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={ "value": 1, "timeframe": tf.get_id() }) setup.assertOk(self, res, 201) data = json.loads(res.data) self.assertIn("id", data) self.assertEqual(str(data['goal']), numeric_goal_id) self.assertEqual(data['value'], 1) self.assertEqual(data['timeframe'], tf.to_dict()) self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data) ## Updated check-in is returned with 200 if Timeframe ID is given, returning correct timeframe res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={ "value": 3, "timeframe": tf.get_id() }) setup.assertOk(self, res, 200) data = json.loads(res.data) self.assertIn("id", data) self.assertEqual(str(data['goal']), numeric_goal_id) self.assertEqual(data['value'], 3) self.assertEqual(data['timeframe'], tf.to_dict()) self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)