Best Python code snippet using hypothesis
message_unpin_test.py
Source:message_unpin_test.py
1import pytest2import requests3import sys4import signal5from json import dumps6from flask import Flask, request7from flask_cors import CORS8from src import config9import json10BASE_URL = config.url11from src.error import InputError, AccessError12@pytest.fixture13def clear_data():14 requests.delete(BASE_URL + 'clear/v1')15 16def new_user(email, password, fname, lname):17 user = requests.post(BASE_URL + 'auth/register/v2', json = {18 'email' : email,19 'password' : password,20 'name_first': fname,21 'name_last': lname,22 })23 return user24def new_channel(name, is_public, token):25 channel = requests.post(BASE_URL + 'channels/create/v2', json = {26 'token': token,27 'name': name,28 'is_public': is_public,29 30 })31 return channel32def dummy_user_channels():33 # Function which creates a new user with u_id 1 and new channel with34 # channel_id 1 by the new user with a message sent35 user1 = new_user("Tony@gmail.com", "2password", "Tony", "Stark").json()36 requests.post(BASE_URL + 'channels/create/v2', json = {37 'token': user1['token'],38 'name': 'General',39 'is_public': True40 })41 requests.post(BASE_URL + 'message/send/v1', json = {42 'token': user1['token'],43 'channel_id': 1,44 'message': 'hello'45 })46 return user1['token']47def dummy_user_dms():48 # Function which creates a dm between 2 people with a message sent by 49 # both users and returns necessary data from both users50 member = new_user("John@gmail.com", "1password", "John", "Smith").json()51 owner = new_user("user1@email.com", "password", "abc", "def").json()52 53 requests.post(BASE_URL + 'dm/create/v1', json = {'token': owner['token'], 'u_ids': [1]})54 requests.post(BASE_URL + 'message/senddm/v1', json = {55 'token': owner['token'],56 'dm_id': 1,57 'message': "hi"58 })59 requests.post(BASE_URL + 'message/senddm/v1', json = {60 'token': member['token'],61 'dm_id': 1,62 'message': "hey"63 })64 65 return [member, owner]66# TESTS67def test_invalid_message_id_channel(clear_data):68 # Test for invalid message id in a channel69 get_token = dummy_user_channels()70 71 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {72 'token': get_token,73 'message_id': 274 })75 assert message_unpin.status_code == 40076 77def test_invalid_message_id_dm(clear_data):78 # Test for invalid message id in a dm79 people = dummy_user_dms()80 81 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {82 'token': people[0]['token'],83 'message_id': 384 })85 assert message_unpin.status_code == 40086 87def test_already_unpinned_channel(clear_data):88 # Test for unpinning a message that is already unpinned in channel89 get_token = dummy_user_channels()90 91 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {92 'token': get_token,93 'message_id': 194 })95 assert message_unpin.status_code == 40096def test_already_unpinned_dm(clear_data):97 # Test for unpinning a message that is already unpinned in dms98 people = dummy_user_dms()99 100 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {101 'token': people[1]['token'],102 'message_id': 1103 })104 assert message_unpin.status_code == 400105def test_not_owner_permission_channel(clear_data):106 # Test for unpinning a pinned message when user does not have permission107 get_token = dummy_user_channels()108 user2 = new_user("user1@email.com", "password", "abc", "def").json()109 110 requests.post(BASE_URL + 'channel/join/v2', json = {111 'token': user2['token'],112 'channel_id': 1113 })114 requests.post(BASE_URL + 'message/pin/v1', json = {115 'token': get_token,116 'message_id': 1117 })118 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {119 'token': user2['token'],120 'message_id': 1121 })122 123 assert message_unpin.status_code == 400124def test_not_owner_permission_dm(clear_data):125 # Test for unpinning a pinned message when user does not have permission126 people = dummy_user_dms()127 requests.post(BASE_URL + 'message/pin/v1', json = {128 'token': people[1]['token'],129 'message_id': 1130 })131 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {132 'token': people[0]['token'],133 'message_id': 1134 })135 136 assert message_unpin.status_code == 400137def test_valid_unpin_channel(clear_data):138 # Simple test for a user to unpin a message they pinned139 get_token = dummy_user_channels()140 requests.post(BASE_URL + 'message/pin/v1', json = {141 'token': get_token,142 'message_id': 1143 })144 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {145 'token': get_token,146 'message_id': 1147 })148 149 assert message_unpin.status_code == 200150def test_valid_unpin_promotion_channel(clear_data):151 # Simple valid case where user joins and is promotes to unpin a pinned message152 get_token = dummy_user_channels()153 user2 = new_user("user1@email.com", "password", "abc", "def").json()154 requests.post(BASE_URL + 'channel/join/v2', json = {155 'token': user2['token'],156 'channel_id': 1157 })158 159 requests.post(BASE_URL + 'channel/addowner/v1', json = {160 'token': get_token, 161 'channel_id': 1,162 'u_id': 2163 })164 requests.post(BASE_URL + 'message/pin/v1', json = {165 'token': get_token,166 'message_id': 1167 })168 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {169 'token': user2['token'],170 'message_id': 1171 })172 assert message_unpin.status_code == 200173def test_valid_unpin_dm(clear_data):174 # Simple valid case of pinning and unpinning in dms175 people = dummy_user_dms()176 requests.post(BASE_URL + 'message/pin/v1', json = {177 'token': people[1]['token'],178 'message_id': 1179 })180 message_unpin = requests.post(BASE_URL + 'message/unpin/v1', json = {181 'token': people[1]['token'],182 'message_id': 1183 })184 assert message_unpin.status_code == 200...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!