def test_update_post_with_auth(): # Редактирование (перед этим добавление) с авторизацией пользователя и без, удаление поста. post = { 'title': 'Lorem ipsum dolor sit amet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_add = json.loads(response.data) assert data_after_add['title'] == post['title'] id = data_after_add['id'] post_update_title = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', } response = client.put('/api/v1/posts/' + str(id), json=post_update_title) assert response.status_code == 401 response = client.put('/api/v1/posts/' + str(id), json=post_update_title, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_update = json.loads(response.data) assert data_after_update['title'] == post_update_title['title'] assert data_after_add['title'] != data_after_update['title'] response = client.delete('/api/v1/posts/' + str(id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204
def test_update_teacher(self): self.test_post_teacher() data = {} response = client.put( '/teach', data=data, headers={ 'Content-Type': 'application/json', 'Authorization': 'Basic ' + base64.b64encode( '[email protected]:qwerty'.encode()).decode() }) self.assertEqual(response.status_code, 405) data = { 'firstname': 'first', 'lastname': 'ahahhah', 'password': '******' } encoded_data = json.dumps(data).encode('utf-8') response = client.put( '/teach', data=encoded_data, headers={ 'Content-Type': 'application/json', 'Authorization': 'Basic ' + base64.b64encode( '[email protected]:qwerty'.encode()).decode() }) self.assertEqual(response.status_code, 200)
def index(): if request.method == 'POST' and request.recaptcha_is_valid: current_user.username = request.form['captcha'] entity = datastore.Entity( key=client.key('User', current_user.username)) entity.update({ 'username': current_user.username, 'Name': current_user.name, 'email': current_user.email, }) client.put(entity) return render_template('index.html')
def test_update_datetime_of_post(): # Добавление и обновление даты публикации поста post = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_add = json.loads(response.data) id = data_after_add['id'] post_update = { 'title': '143223', 'publication_datetime': '2021-02-03T13:48:05.686613' } response = client.put('/api/v1/posts/' + str(id), json=post_update, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_update = json.loads(response.data) assert data_after_update['publication_datetime'] == post_update[ 'publication_datetime'] assert data_after_update['publication_datetime'] != data_after_add[ 'publication_datetime'] response = client.delete('/api/v1/posts/' + str(id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204
def test_update_not_user_post(): # Пост добавляется одним пользователем, а попытка изменить другим post = { 'title': 'Lorem ipsum dolor sit amet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_add = json.loads(response.data) id = data_after_add['id'] post_update_title = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', } another_user = base64.b64encode(b"4:44").decode("utf-8") response = client.put('/api/v1/posts/' + str(id), json=post_update_title, headers={"Authorization": "Basic " + another_user}) assert response.status_code == 400 error = {'message': 'No post with this id'} data = json.loads(response.data) assert data == error response = client.put('/api/v1/posts/' + str(id), json=post_update_title, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 response = client.delete('/api/v1/posts/' + str(id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204
def test_update_comment(): # Добавление комментария пользователем к созданном посту, обновление комментария не авторизованным пользователем, авторизованным, # но не автором комметария, и автором комментария # удаление комментраия автором post = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_add = json.loads(response.data) id = data_after_add['id'] comment = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', } response = client.post('/api/v1/posts/' + str(id) + '/comments', json=comment, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_add = json.loads(response.data) comment_id = data_after_add['id'] update_comment = { 'title': 'Lor423434322em fdsfds342ipsum dolor sit aet.', } response = client.put('/api/v1/posts/' + str(id) + '/comments/' + str(comment_id), json=update_comment) assert response.status_code == 401 another_user = base64.b64encode(b"4:44").decode("utf-8") response = client.put('/api/v1/posts/' + str(id) + '/comments/' + str(comment_id), json=update_comment, headers={"Authorization": "Basic " + another_user}) assert response.status_code == 400 error = {'message': 'No comment with this id'} data = json.loads(response.data) assert data == error response = client.put('/api/v1/posts/' + str(id) + '/comments/' + str(comment_id), json=update_comment, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 200 data_after_update = json.loads(response.data) assert data_after_update['title'] == update_comment['title'] assert data_after_update['title'] != data_after_add['title'] response = client.delete('/api/v1/posts/' + str(id) + '/comments/' + str(comment_id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204 response = client.delete('/api/v1/posts/' + str(id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204
def test_put(): res = client.put('/region/1', json={'name': 'Южный'}) assert res.status_code == 200 assert Region.query.get(1).name == 'Центральный'