Best Python code snippet using localstack_python
test_goal_api.py
Source:test_goal_api.py
...19 def test_create_goal(self):20 """21 Should be able to create a goal22 """23 bucket = Utils.create_test_bucket(user=self.user)24 payload = Utils.get_test_goal(bucket=bucket.id)25 response = self.client.post(26 "/api/goal/", payload, format="json", **self.header_token27 )28 self.assertEqual(29 response.status_code, HTTPStatus.CREATED._value_, msg=response.reason_phrase30 )31 self.assertIn("id", response.data)32 for key in payload:33 self.assertEqual(response.data[key], payload[key])34 def test_create_goal_should_validate_user(self):35 """36 Should not be able to create a goal for another users bucket37 """38 bucket = Utils.create_test_bucket()39 payload = Utils.get_test_goal()40 response = self.client.post(41 "/api/goal/", payload, format="json", **self.header_token42 )43 self.assertEqual(44 response.status_code,45 HTTPStatus.BAD_REQUEST._value_,46 msg=response.reason_phrase,47 )48 def test_create_goal_should_validate_user_logged_in(self):49 """50 Should only be able to create a goal when logged in51 """52 bucket = Utils.create_test_bucket(user=self.user)53 payload = Utils.get_test_goal(bucket=bucket.id)54 response = self.client.post("/api/goal/", payload, format="json")55 self.assertEqual(response.status_code, HTTPStatus.UNAUTHORIZED._value_)56 response = self.client.post(57 "/api/goal/", payload, format="json", **self.header_token58 )59 self.assertEqual(response.status_code, HTTPStatus.CREATED._value_)60 def test_list_goals(self):61 """62 Should only be able to list the goals of the current user63 """64 user_bucket = Utils.create_test_bucket(user=self.user)65 other_bucket = Utils.create_test_bucket()66 user_goals = Utils.create_test_goals(len=5, bucket=user_bucket)67 other_goals = Utils.create_test_goals(len=3, bucket=other_bucket)68 total_num_of_goals = len(user_goals) + len(other_goals)69 self.assertEqual(Goal.objects.count(), total_num_of_goals)70 response = self.client.get("/api/goal/", **self.header_token)71 self.assertEqual(response.status_code, HTTPStatus.OK._value_)72 data = response.data73 self.assertEqual(len(data), len(user_goals))74 def test_retrieve_goals(self):75 """76 Should only be able to retrieve the goals of the current user77 """78 user_bucket = Utils.create_test_bucket(user=self.user)79 other_bucket = Utils.create_test_bucket()80 user_goals = Utils.create_test_goals(len=5, bucket=user_bucket)81 other_goals = Utils.create_test_goals(len=3, bucket=other_bucket)82 for goal in user_goals:83 response = self.client.get(f"/api/goal/{goal.id}/", **self.header_token)84 self.assertEqual(response.status_code, HTTPStatus.OK._value_)85 for goal in other_goals:86 response = self.client.get(f"/api/goal/{goal.id}/", **self.header_token)87 self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND._value_)88 def test_patch_goal(self):89 """90 Should be able to update own goals91 """92 user_bucket = Utils.create_test_bucket(user=self.user)93 user_goals = Utils.create_test_goals(len=5, bucket=user_bucket)94 for goal in user_goals:95 payload = {96 "id": goal.id,97 "name": Utils.get_random_string(),98 }99 response = self.client.patch(100 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token101 )102 self.assertEqual(response.status_code, HTTPStatus.OK._value_)103 self.assertEqual(response.data["id"], goal.id)104 self.assertEqual(response.data["name"], payload["name"])105 def test_patch_bucket_should_validate_user(self):106 """107 Should not be able to update others goals108 """109 other_bucket = Utils.create_test_bucket()110 other_goals = Utils.create_test_goals(len=3, bucket=other_bucket)111 for goal in other_goals:112 payload = payload = {113 "id": goal.id,114 "name": Utils.get_random_string(),115 }116 response = self.client.patch(117 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token118 )119 self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND._value_)120 def test_put_goal(self):121 """122 Should be able to put own goals123 """124 user_bucket = Utils.create_test_bucket(user=self.user)125 user_goals = Utils.create_test_goals(len=5, bucket=user_bucket)126 for goal in user_goals:127 payload = Utils.get_test_goal(id=goal.id, bucket=user_bucket.id)128 response = self.client.put(129 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token130 )131 self.assertEqual(response.status_code, HTTPStatus.OK._value_)132 self.assertEqual(response.data["id"], goal.id)133 self.assertEqual(response.data["name"], payload["name"])134 def test_put_goal_should_validate_user(self):135 """136 Should not be able to update others goals137 """138 other_bucket = Utils.create_test_bucket()139 other_goals = Utils.create_test_goals(len=3, bucket=other_bucket)140 for goal in other_goals:141 payload = Utils.get_test_goal(id=goal.id, bucket=other_bucket.id)142 response = self.client.put(143 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token144 )145 self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND._value_)146 def test_change_goal_bucket(self):147 """148 Should be able to change a goals bucket149 """150 # create two buckets151 user_buckets = Utils.create_test_buckets(user=self.user, len=2)152 goal = Utils.create_test_goal(bucket=user_buckets[0])153 # move goal to the second bucket154 payload = Utils.get_test_goal(id=goal.id, bucket=user_buckets[1].id)155 response = self.client.patch(156 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token157 )158 self.assertEqual(response.status_code, HTTPStatus.OK._value_)159 def test_cannot_change_goal_to_other_users_bucket(self):160 """161 Should not be able to move a goal to another users bucket162 """163 user_bucket = Utils.create_test_bucket(user=self.user)164 other_bucket = Utils.create_test_bucket()165 goal = Utils.create_test_goal(bucket=user_bucket)166 # move goal to the other users bucket167 payload = Utils.get_test_goal(id=goal.id, bucket=other_bucket.id)168 response = self.client.patch(169 f"/api/goal/{goal.id}/", payload, format="json", **self.header_token170 )171 self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST._value_)172 def test_can_delete_goal(self):173 """174 Should be able to delete own goals175 """176 user_bucket = Utils.create_test_bucket(user=self.user)177 goal = Utils.create_test_goal(bucket=user_bucket)178 response = self.client.get(f"/api/goal/{goal.id}/", **self.header_token)179 self.assertEqual(response.status_code, HTTPStatus.OK)180 response = self.client.delete(f"/api/goal/{goal.id}/", **self.header_token)181 self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)182 response = self.client.get(f"/api/goal/{goal.id}/", **self.header_token)183 self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)184 def test_cannot_delete_others_goals(self):185 """186 Should not be able to delete the goals of another user187 """188 bucket = Utils.create_test_bucket()189 goal = Utils.create_test_goal(bucket=bucket)190 response = self.client.delete(f"/api/goal/{goal.id}/", **self.header_token)...
test_put_bucket_object.py
Source:test_put_bucket_object.py
...7 return S3Client()8@pytest.fixture()9def boto3_client():10 return boto3.client('s3')11def create_test_bucket(name='test'):12 """ Create a test S3 bucket."""13 boto3_client().create_bucket(Bucket=name)14 return name15@moto.mock_s3()16def test_put_bucket_object(client, boto3_client):17 """ Test putting an object in an S3 bucket."""18 test_bucket = create_test_bucket()19 client.save_str_file('sample', test_bucket, 'sample.txt', 'text/plain')20 boto3_client = boto3.client('s3')21 item = boto3_client.get_object(22 Bucket=test_bucket,23 Key='sample.txt'24 )...
test_s3_image.py
Source:test_s3_image.py
1from s3_image import _get_gps_info_s32from conftest import create_test_bucket3def test_get_gps_info(s3):4 create_test_bucket(s3)5 info = _get_gps_info_s3('tree-upload', 'tree-1000px.jpg')6 assert info['Latitude'] == 60.228088888888897 assert info['Longitude'] == 24.851588888888898 assert info['Altitude'] == 74.007...
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!!