def raise_for_response(cls, response): message = f"Received status code {response.status_code}" try: doc = response.json() except ValueError: raise TransportError(message) # If the error payload contains the migrate_to_chat_id field, # raise MigrationRequiredError, with the new chat_id included try: jsonschema.validate(doc, telegram_migration) description = doc["description"] chat_id = doc["parameters"]["migrate_to_chat_id"] raise MigrationRequiredError(description, chat_id) except jsonschema.ValidationError: pass permanent = False description = doc.get("description") if isinstance(description, str): message += f' with a message: "{description}"' if description == "Forbidden: the group chat was deleted": permanent = True raise TransportError(message, permanent=permanent)
def test_it_validates_arrays(self): validate(["foo", "bar"], { "type": "array", "items": { "type": "string" } })
def wrapper(request, *args, **kwds): try: validate(request.json, schema) except ValidationError as e: return make_error("json validation error: %s" % e) return f(request, *args, **kwds)
def test_it_validates_objects(self): validate({"foo": "bar"}, { "type": "object", "properties": { "foo": {"type": "string"} } })
def test_it_validates_objects_properties(self): with self.assertRaises(ValidationError): validate({"foo": "bar"}, { "type": "object", "properties": { "foo": {"type": "number"} } })
def test_it_validates_array_type(self): with self.assertRaises(ValidationError): validate("not-an-array", { "type": "array", "items": { "type": "string" } })
def test_it_validates_array_elements(self): with self.assertRaises(ValidationError): validate(["foo", "bar"], { "type": "array", "items": { "type": "number" } })
def test_it_validates_objects(self): validate({"foo": "bar"}, { "type": "object", "properties": { "foo": { "type": "string" } } })
def test_it_validates_objects_properties(self): with self.assertRaises(ValidationError): validate({"foo": "bar"}, { "type": "object", "properties": { "foo": { "type": "number" } } })
def wrapper(request, *args, **kwds): if request.method == "POST" and request.body: try: request.json = json.loads(request.body.decode()) except ValueError: return error("could not parse request body") else: request.json = {} try: validate(request.json, schema) except ValidationError as e: return error("json validation error: %s" % e) return f(request, *args, **kwds)
def telegram_bot(request): try: doc = json.loads(request.body.decode("utf-8")) jsonschema.validate(doc, telegram_callback) except ValueError: return HttpResponseBadRequest() except jsonschema.ValidationError: return HttpResponseBadRequest() if "/start" not in doc["message"]["text"]: return HttpResponse() chat = doc["message"]["chat"] name = max(chat.get("title", ""), chat.get("username", "")) invite = render_to_string("integrations/telegram_invite.html", { "qs": signing.dumps((chat["id"], chat["type"], name)) }) Telegram.send(chat["id"], invite) return HttpResponse()
def telegram_bot(request): try: doc = json.loads(request.body.decode("utf-8")) jsonschema.validate(doc, telegram_callback) except ValueError: return HttpResponseBadRequest() except jsonschema.ValidationError: return HttpResponseBadRequest() if "/start" not in doc["message"]["text"]: return HttpResponse() chat = doc["message"]["chat"] name = max(chat.get("title", ""), chat.get("username", "")) invite = render_to_string( "integrations/telegram_invite.html", {"qs": signing.dumps((chat["id"], chat["type"], name))}) Telegram.send(chat["id"], invite) return HttpResponse()
def telegram_bot(request): try: doc = json.loads(request.body.decode()) jsonschema.validate(doc, telegram_callback) except ValueError: return HttpResponseBadRequest() except jsonschema.ValidationError: # We don't recognize the message format, but don't want Telegram # retrying this over and over again, so respond with 200 OK return HttpResponse() if "/start" not in doc["message"]["text"]: return HttpResponse() chat = doc["message"]["chat"] name = max(chat.get("title", ""), chat.get("username", "")) invite = render_to_string( "integrations/telegram_invite.html", {"qs": signing.dumps((chat["id"], chat["type"], name))}) Telegram.send(chat["id"], invite) return HttpResponse()
def single(request, code): check = get_object_or_404(Check, code=code) if check.project != request.project: return HttpResponseForbidden() if request.method == "POST": try: validate(request.json, schemas.check) _update(check, request.json) except (BadChannelException, ValidationError) as e: return JsonResponse({"error": str(e)}, status=400) return JsonResponse(check.to_dict()) elif request.method == "DELETE": response = check.to_dict() check.delete() return JsonResponse(response) elif request.method == "GET": return JsonResponse(check.to_dict()) # Otherwise, method not allowed return HttpResponse(status=405)
def test_it_validates_numbers(self): validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
def test_it_checks_string_length(self): with self.assertRaises(ValidationError): validate("abcd", {"type": "string", "maxLength": 3})
def test_it_checks_timezone_format(self): with self.assertRaises(ValidationError): validate("X/Y", {"type": "string", "format": "timezone"})
def test_it_handles_required_properties(self): with self.assertRaises(ValidationError): validate({"foo": "bar"}, {"type": "object", "required": ["baz"]})
def test_it_checks_boolean_type(self): with self.assertRaises(ValidationError): validate(123, {"type": "boolean"})
def test_it_checks_string_type(self): with self.assertRaises(ValidationError): validate(123, {"type": "string"})
def test_it_rejects_a_value_not_in_enum(self): with self.assertRaises(ValidationError): validate("baz", {"enum": ["foo", "bar"]})
def test_it_validates_strings(self): validate("foo", {"type": "string"})
def test_it_validates_array_type(self): with self.assertRaises(ValidationError): validate("not-an-array", { "type": "array", "items": {"type": "string"} })
def test_it_validates_arrays(self): validate(["foo", "bar"], { "type": "array", "items": {"type": "string"} })
def test_it_handles_required_properties(self): with self.assertRaises(ValidationError): validate({"foo": "bar"}, { "type": "object", "required": ["baz"] })
def test_it_checks_int_type(self): with self.assertRaises(ValidationError): validate("foo", {"type": "number"})
def test_it_checks_max_value(self): with self.assertRaises(ValidationError): validate(5, {"type": "number", "maximum": 0})
def test_it_checks_cron_format(self): with self.assertRaises(ValidationError): validate("x * * * *", {"type": "string", "format": "cron"})
def test_it_checks_dict_type(self): with self.assertRaises(ValidationError): validate("not-object", {"type": "object"})
def test_it_checks_cron_format(self): samples = ["x * * * *", "0 0 31 2 *", "* * * * * *"] for sample in samples: with self.assertRaises(ValidationError): validate(sample, {"type": "string", "format": "cron"})
def test_it_validates_array_elements(self): with self.assertRaises(ValidationError): validate(["foo", "bar"], { "type": "array", "items": {"type": "number"} })
def test_it_validates_enum(self): validate("foo", {"enum": ["foo", "bar"]})