def test_post_student(self): with app.test_client() as client: error_response = client.post( '/stud', headers={ 'Content-Type': 'any', 'Authorization': 'Basic ' + base64.b64encode( '[email protected]:qwerty'.encode()).decode() }) self.assertEqual(error_response.status_code, 405) data = { 'firstname': 'first', 'lastname': 'second', 'email': '*****@*****.**', 'password': '******' } encoded_data = json.dumps(data).encode('utf-8') response = client.post( '/stud', data=encoded_data, headers={'Content-Type': 'application/json'}) second_response = client.post( '/stud', data=encoded_data, headers={'Content-Type': 'application/json'}) self.assertEqual(response.status_code, 200) self.assertEqual(second_response.status_code, 401)
def test_delete_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'] response = client.delete('/api/v1/posts/' + str(id) + '/comments/' + str(comment_id)) assert response.status_code == 401 another_user = base64.b64encode(b"4:44").decode("utf-8") response = client.delete( '/api/v1/posts/' + str(id) + '/comments/' + str(comment_id), 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.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_view_login(): res = client.get('/login') assert res.status_code == 200 data = {'username': MAIN_DATA[0], 'password': MAIN_DATA[1]} res = client.post('/login', json=data) assert res.status_code == 200
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_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_put(): input_data = [ { 'id': 0, 'lesson': [0, 1], 'pupil': [ 1594663340, 1594663389, 1594663390, 1594663395, 1594663396, 1594666472 ], 'tutor': [1594663290, 1594663430, 1594663443, 1594666473] }, { 'id': 1, 'lesson': [0, 1000], 'pupil': [100, 200, 400, 650, 780, 920], 'tutor': [150, 250, 600, 900] }, { 'id': 2, 'lesson': [0, 1000], 'pupil': [100, 200, 400, 650, 780, 920], # Skip 'tutor' parameter } ] res = client.post('/api', json=input_data) assert res.status_code == 200 assert len(res.get_json()) == 3 assert res.json[0]["second"] == "0" assert res.json[0]["error_message"] == "Success" assert res.json[1]["second"] == "223" assert res.json[2]["error_message"] == "Wrong format data"
def test_post(): data = { 'id': 3, 'tittle': '3333', 'descriptions': '3333', } res = client.post('/web', json=data) assert res.status_code == 200 assert len(res.get_json()) == 3 assert res.get_json()[-1]['tittle'] == data['tittle']
def test_add_post_without_auth(): # Добавление поста без авторизации post = { 'title': 'Lorem ipsum dolor sit amet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet lectus ut quam ullamcorper laoreet. In vitae lectus suscipit ex suscipit accumsan. Mauris condimentum consectetur lectus ac sollicitudin. Nulla a mi tristique, bibendum arcu finibus, faucibus tellus. Maecenas elit arcu, cursus quis varius quis, efficitur eu lectus. Cras ac nulla ante. Maecenas posuere euismod augue, id mattis risus accumsan vitae.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet lectus ut quam ullamcorper laoreet. In vitae lectus suscipit ex suscipit accumsan. Mauris condimentum consectetur lectus ac sollicitudin. Nulla a mi tristique, bibendum arcu finibus, faucibus tellus. Maecenas elit arcu, cursus quis varius quis, efficitur eu lectus. Cras ac nulla ante. Maecenas posuere euismod augue, id mattis risus accumsan vitae.', } response = client.post('/api/v1/posts', json=post) assert response.status_code == 401
def test_view_registration(): res = client.get('/registration') assert res.status_code == 200 data = { 'username': MAIN_DATA[0], 'password1': MAIN_DATA[1], 'password2': MAIN_DATA[2], } res = client.post('/registration', json=data) assert res.status_code == 200 data = { 'username': MAIN_DATA[0], 'password1': MAIN_DATA[1], 'password2': 'gakdofg3333', } res = client.post('/registration', json=data) assert res.status_code == 404
def test_add_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'] 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_add_post_without_content(): # Добавление поста, в содержимом только заголовок без поля content post = { 'title': 'Lor423432em fdsfds342ipsum dolor sit aet.', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 400 error = { "message": { "json": { "content": ["Missing data for required field."] } } } data = json.loads(response.data) assert data == error
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_add_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 = json.loads(response.data) assert data['content'] == post['content'] assert data['title'] == post['title'] id = data['id'] response = client.delete('/api/v1/posts/' + str(id)) assert response.status_code == 401 response = client.delete('/api/v1/posts/' + str(id), headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 204
def test_add_post_with_auth_with_error_with_content_length(): # Добавление поста с авторизацией пользователя. В данных есть ошибка, длина content больше 500 post = { 'title': 'Lorem ipsum dolor sit amet.', 'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet lectus ut quam ullamcorper laoreet. In vitae lectus suscipit ex suscipit accumsan. Mauris condimentum consectetur lectus ac sollicitudin. Nulla a mi tristique, bibendum arcu finibus, faucibus tellus. Maecenas elit arcu, cursus quis varius quis, efficitur eu lectus. Cras ac nulla ante. Maecenas posuere euismod augue, id mattis risus accumsan vitae.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer aliquet lectus ut quam ullamcorper laoreet. In vitae lectus suscipit ex suscipit accumsan. Mauris condimentum consectetur lectus ac sollicitudin. Nulla a mi tristique, bibendum arcu finibus, faucibus tellus. Maecenas elit arcu, cursus quis varius quis, efficitur eu lectus. Cras ac nulla ante. Maecenas posuere euismod augue, id mattis risus accumsan vitae.', } response = client.post('/api/v1/posts', json=post, headers={"Authorization": "Basic " + user_auth}) assert response.status_code == 400 error = { "message": { "json": { "content": ["Longer than maximum length 500."] } } } data = json.loads(response.data) assert data == error
def test_post(): data = {'name': 'Восточный', 'region_id': 'None'} res = client.post('/region', json=data) assert res.status_code == 200