def test_user_update(self): api_key = self.s.sign(self.api_key.name) ''' UPDATE ''' post_data = { 'name': 'New Person', 'email': '*****@*****.**', 'password': '******', 'id': self.user.id } # Try to update the user with no API key rv = self.app.post('/user_update', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # update the user. This should work fine. post_data['api_key'] = api_key rv = self.app.post('/user_update', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['user']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'The user was updated.') # Make sure that we can grab the user from the DB. user = User.get(self.user.id) self.assertIsNotNone(user) self.assertEquals(data['user']['name'], 'New Person') self.assertEquals(user.name, 'New Person') self.assertTrue(check_password_hash(user.password, 'newperson123'))
def admin_users_edit_post(): payload = get_payload(request) user_id = payload.get('user_id') if user_id: user = User.get(user_id) return_value = success('All profile values have been updated.') else: user = User() user.insert() return_value = success('User created.') if not payload.get('password'): return jsonify(failure('You must set a password for new users')) if not payload.get('email'): return jsonify(failure('You must set an email for new users')) if not payload.get('name'): return jsonify(failure('You must set a name for new users')) for key in payload: if key == 'password': user.set_password(payload[key]) elif key != 'user_id': setattr(user, key, payload[key]) g.user.insert() safe_commit() return jsonify(return_value)
def retrieve_user(): return_value = success('The user was retrieved.') payload = get_payload(request) user = User.get(payload.get('id')) if not user: return_value = failure('That user does not exist.') else: return_value['user'] = user.to_dict(camel_case=True) return_value['user'].pop('password') return_value['user'].pop('openid') return jsonify(return_value)
def update_user(): return_value = success('The user was updated.') payload = get_payload(request) user = User.get(payload.get('id')) if not user: return_value = failure('That user does not exist.') else: if payload.get('password'): user.set_password(payload.get('password')) if payload.get('email'): user.email = payload.get('email') if payload.get('name'): user.name = payload.get('name') safe_commit() return_value['user'] = user.to_dict(camel_case=True) return jsonify(return_value)
def update_user(): return_value = success('The user was updated.') payload = get_payload(request) user = User.get(payload.get('id')) if not user: return_value = failure('That user does not exist.') else: if payload.get('password'): hashed_password = generate_password_hash(payload.get('password')) if payload.get('email'): user.email = payload.get('email') if payload.get('name'): user.name = payload.get('name') user.password = hashed_password safe_commit() return_value['user'] = user.to_dict(camel_case=True) return jsonify(return_value)
def test_user_create(self): api_key = self.s.sign(self.api_key.name) ''' CREATE ''' post_data = { 'name': 'Testy McTesterson', 'email': '*****@*****.**', 'password': '******', } # Try to create the user with no API key rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # Create the user. This should work fine. post_data['api_key'] = api_key rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['id']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'The user was created.') user_id = data['id'] # Make sure that we can grab the user from the DB. user = User.get(user_id) self.assertIsNotNone(user) self.assertEquals(user.name, 'Testy McTesterson') # Try to create the same user again. This should fail. rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'That user exists already.') # Clean up! user.delete() safe_commit()
def test_user_delete(self): api_key = self.s.sign(self.api_key.name) ''' DELETE ''' # Delete the user. post_data = { 'id': self.user.id } # Try to delete the user with no API key rv = self.app.post('/user_delete', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # Removing should work now. post_data['api_key'] = api_key rv = self.app.post('/user_delete', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) user = User.get(self.user.id) self.assertIsNone(user)
def test_user_delete(self): api_key = self.s.sign(self.api_key.name) ''' DELETE ''' # Delete the user. post_data = { 'id': self.user.id } # Try to delete the user with no API key rv = self.app.post('/admin/users/delete', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # Removing should work now. post_data['api_key'] = api_key rv = self.app.post('/user_delete', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) user = User.get(self.user.id) self.assertIsNone(user)
def admin_users_edit(user_id=''): user = User.get(user_id) return render_admin('user.html', user=user)