Example #1
0
    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)
Example #2
0
 def test_it_validates_arrays(self):
     validate(["foo", "bar"], {
         "type": "array",
         "items": {
             "type": "string"
         }
     })
Example #3
0
        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 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)
Example #5
0
 def test_it_validates_objects(self):
     validate({"foo": "bar"}, {
         "type": "object",
         "properties": {
             "foo": {"type": "string"}
         }
     })
Example #6
0
 def test_it_validates_objects_properties(self):
     with self.assertRaises(ValidationError):
         validate({"foo": "bar"}, {
             "type": "object",
             "properties": {
                 "foo": {"type": "number"}
             }
         })
Example #7
0
 def test_it_validates_array_type(self):
     with self.assertRaises(ValidationError):
         validate("not-an-array", {
             "type": "array",
             "items": {
                 "type": "string"
             }
         })
Example #8
0
 def test_it_validates_array_elements(self):
     with self.assertRaises(ValidationError):
         validate(["foo", "bar"], {
             "type": "array",
             "items": {
                 "type": "number"
             }
         })
Example #9
0
 def test_it_validates_objects(self):
     validate({"foo": "bar"}, {
         "type": "object",
         "properties": {
             "foo": {
                 "type": "string"
             }
         }
     })
Example #10
0
 def test_it_validates_objects_properties(self):
     with self.assertRaises(ValidationError):
         validate({"foo": "bar"}, {
             "type": "object",
             "properties": {
                 "foo": {
                     "type": "number"
                 }
             }
         })
Example #11
0
        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)
Example #12
0
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()
Example #13
0
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()
Example #14
0
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()
Example #15
0
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)
Example #16
0
 def test_it_validates_numbers(self):
     validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
Example #17
0
 def test_it_checks_string_length(self):
     with self.assertRaises(ValidationError):
         validate("abcd", {"type": "string", "maxLength": 3})
Example #18
0
 def test_it_checks_timezone_format(self):
     with self.assertRaises(ValidationError):
         validate("X/Y", {"type": "string", "format": "timezone"})
Example #19
0
 def test_it_validates_numbers(self):
     validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
Example #20
0
 def test_it_handles_required_properties(self):
     with self.assertRaises(ValidationError):
         validate({"foo": "bar"}, {"type": "object", "required": ["baz"]})
Example #21
0
 def test_it_checks_boolean_type(self):
     with self.assertRaises(ValidationError):
         validate(123, {"type": "boolean"})
Example #22
0
 def test_it_checks_string_type(self):
     with self.assertRaises(ValidationError):
         validate(123, {"type": "string"})
Example #23
0
 def test_it_checks_string_length(self):
     with self.assertRaises(ValidationError):
         validate("abcd", {"type": "string", "maxLength": 3})
Example #24
0
 def test_it_rejects_a_value_not_in_enum(self):
     with self.assertRaises(ValidationError):
         validate("baz", {"enum": ["foo", "bar"]})
Example #25
0
 def test_it_checks_string_type(self):
     with self.assertRaises(ValidationError):
         validate(123, {"type": "string"})
Example #26
0
 def test_it_validates_strings(self):
     validate("foo", {"type": "string"})
Example #27
0
 def test_it_validates_array_type(self):
     with self.assertRaises(ValidationError):
         validate("not-an-array", {
             "type": "array",
             "items": {"type": "string"}
         })
Example #28
0
 def test_it_validates_arrays(self):
     validate(["foo", "bar"], {
         "type": "array",
         "items": {"type": "string"}
     })
Example #29
0
 def test_it_handles_required_properties(self):
     with self.assertRaises(ValidationError):
         validate({"foo": "bar"}, {
             "type": "object",
             "required": ["baz"]
         })
Example #30
0
 def test_it_checks_int_type(self):
     with self.assertRaises(ValidationError):
         validate("foo", {"type": "number"})
Example #31
0
 def test_it_checks_max_value(self):
     with self.assertRaises(ValidationError):
         validate(5, {"type": "number", "maximum": 0})
Example #32
0
 def test_it_checks_cron_format(self):
     with self.assertRaises(ValidationError):
         validate("x * * * *", {"type": "string", "format": "cron"})
Example #33
0
 def test_it_checks_dict_type(self):
     with self.assertRaises(ValidationError):
         validate("not-object", {"type": "object"})
Example #34
0
 def test_it_checks_timezone_format(self):
     with self.assertRaises(ValidationError):
         validate("X/Y", {"type": "string", "format": "timezone"})
Example #35
0
 def test_it_validates_strings(self):
     validate("foo", {"type": "string"})
Example #36
0
 def test_it_checks_int_type(self):
     with self.assertRaises(ValidationError):
         validate("foo", {"type": "number"})
Example #37
0
 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"})
Example #38
0
 def test_it_checks_dict_type(self):
     with self.assertRaises(ValidationError):
         validate("not-object", {"type": "object"})
Example #39
0
 def test_it_validates_array_elements(self):
     with self.assertRaises(ValidationError):
         validate(["foo", "bar"], {
             "type": "array",
             "items": {"type": "number"}
         })
Example #40
0
 def test_it_checks_max_value(self):
     with self.assertRaises(ValidationError):
         validate(5, {"type": "number", "maximum": 0})
Example #41
0
 def test_it_validates_enum(self):
     validate("foo", {"enum": ["foo", "bar"]})
Example #42
0
 def test_it_validates_enum(self):
     validate("foo", {"enum": ["foo", "bar"]})
Example #43
0
 def test_it_checks_cron_format(self):
     with self.assertRaises(ValidationError):
         validate("x * * * *", {"type": "string", "format": "cron"})
Example #44
0
 def test_it_rejects_a_value_not_in_enum(self):
     with self.assertRaises(ValidationError):
         validate("baz", {"enum": ["foo", "bar"]})