Example #1
0
 def test_parse_day_arg(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
     with self.subTest("single"):
         with app.test_request_context("/?time=*"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
         with app.test_request_context("/?time=20201201"):
             self.assertEqual(parse_day_arg("time"), 20201201)
     with self.subTest("single list"):
         with app.test_request_context("/?time=20201201,20201202"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
     with self.subTest("single range"):
         with app.test_request_context("/?time=20201201-20201204"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
     with self.subTest("wrong"):
         with app.test_request_context("/?time=abc"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
         with app.test_request_context("/?time=sig=4"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
         with app.test_request_context("/?time=month:201210"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
         with app.test_request_context("/?time=week:20121010"):
             self.assertRaises(ValidationFailedException, parse_day_arg,
                               "time")
Example #2
0
    def test_extract_integers(self):
        with self.subTest("empty"):
            with app.test_request_context("/"):
                self.assertIsNone(extract_integers("s"))
        with self.subTest("single"):
            with app.test_request_context("/?s=1"):
                self.assertEqual(extract_integers("s"), [1])
        with self.subTest("multiple"):
            with app.test_request_context("/?s=1,2"):
                self.assertEqual(extract_integers("s"), [1, 2])
        with self.subTest("multiple param"):
            with app.test_request_context("/?s=1&s=2"):
                self.assertEqual(extract_integers("s"), [1, 2])
        with self.subTest("multiple param mixed"):
            with app.test_request_context("/?s=1&s=2,3"):
                self.assertEqual(extract_integers("s"), [1, 2, 3])

        with self.subTest("not a number"):
            with app.test_request_context("/?s=a"):
                self.assertRaises(ValidationFailedException,
                                  lambda: extract_integers("s"))

        with self.subTest("simple range"):
            with app.test_request_context("/?s=1-2"):
                self.assertEqual(extract_integers("s"), [(1, 2)])
        with self.subTest("inverted range"):
            with app.test_request_context("/?s=2-1"):
                self.assertRaises(ValidationFailedException,
                                  lambda: extract_integers("s"))
        with self.subTest("single range"):
            with app.test_request_context("/?s=1-1"):
                self.assertEqual(extract_integers("s"), [1])
Example #3
0
 def test_extract_integer(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertIsNone(extract_integer("s"))
     with self.subTest("single"):
         with app.test_request_context("/?s=1"):
             self.assertEqual(extract_integer("s"), 1)
     with self.subTest("not a number"):
         with app.test_request_context("/?s=a"):
             self.assertRaises(ValidationFailedException,
                               lambda: extract_integer("s"))
Example #4
0
 def test_extract_date(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertIsNone(extract_date("s"))
     with self.subTest("single"):
         with app.test_request_context("/?s=2020-01-01"):
             self.assertEqual(extract_date("s"), 20200101)
         with app.test_request_context("/?s=20200101"):
             self.assertEqual(extract_date("s"), 20200101)
     with self.subTest("not a date"):
         with app.test_request_context("/?s=abc"):
             self.assertRaises(ValidationFailedException,
                               lambda: extract_date("s"))
Example #5
0
 def test_require_all(self):
     with self.subTest("all given"):
         with app.test_request_context("/"):
             self.assertTrue(require_all())
         with app.test_request_context("/?abc=abc&def=3"):
             self.assertTrue(require_all("abc", "def"))
     with self.subTest("missing parameter"):
         with app.test_request_context("/?abc=abc"):
             self.assertRaises(ValidationFailedException,
                               lambda: require_all("abc", "def"))
     with self.subTest("missing empty parameter"):
         with app.test_request_context("/?abc=abc&def="):
             self.assertRaises(ValidationFailedException,
                               lambda: require_all("abc", "def"))
Example #6
0
    def test_parse_day_value(self):
        with app.test_request_context(""):
            with self.subTest("delphi date"):
                self.assertEqual(parse_day_value("20200101"), 20200101)
            with self.subTest("iso date"):
                self.assertEqual(parse_day_value("2020-01-01"), 20200101)
            with self.subTest("delphi date range"):
                self.assertEqual(parse_day_value("20200101-20210404"),
                                 (20200101, 20210404))
            with self.subTest("iso date range"):
                self.assertEqual(parse_day_value("2020-01-01--2021-04-04"),
                                 (20200101, 20210404))

            with self.subTest("wrong"):
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "")
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "x")
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "2020")
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "202001")
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "2020-03-111")
                self.assertRaises(ValidationFailedException, parse_day_value,
                                  "2020-02-30---20200403")
Example #7
0
 def test_extract_strings(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertIsNone(extract_strings("s"))
     with self.subTest("single"):
         with app.test_request_context("/?s=a"):
             self.assertEqual(extract_strings("s"), ["a"])
     with self.subTest("multiple"):
         with app.test_request_context("/?s=a,b"):
             self.assertEqual(extract_strings("s"), ["a", "b"])
     with self.subTest("multiple param"):
         with app.test_request_context("/?s=a&s=b"):
             self.assertEqual(extract_strings("s"), ["a", "b"])
     with self.subTest("multiple param mixed"):
         with app.test_request_context("/?s=a&s=b,c"):
             self.assertEqual(extract_strings("s"), ["a", "b", "c"])
Example #8
0
 def test_check_auth_token(self):
     with self.subTest("no auth but optional"):
         with app.test_request_context("/"):
             self.assertFalse(check_auth_token("abc", True))
     with self.subTest("no auth but required"):
         with app.test_request_context("/"):
             self.assertRaises(ValidationFailedException,
                               lambda: check_auth_token("abc"))
     with self.subTest("auth and required"):
         with app.test_request_context("/?auth=abc"):
             self.assertTrue(check_auth_token("abc"))
     with self.subTest("auth and required but wrong"):
         with app.test_request_context("/?auth=abc"):
             self.assertRaises(UnAuthenticatedException,
                               lambda: check_auth_token("def"))
     with self.subTest("auth and required but wrong but optional"):
         with app.test_request_context("/?auth=abc"):
             self.assertFalse(check_auth_token("def", True))
Example #9
0
 def test_require_any(self):
     with self.subTest("default given"):
         with app.test_request_context("/"):
             self.assertRaises(ValidationFailedException,
                               lambda: require_any("abc"))
     with self.subTest("one option give"):
         with app.test_request_context("/?abc=abc"):
             self.assertTrue(require_any("abc", "def"))
     with self.subTest("multiple options given"):
         with app.test_request_context("/?abc=abc&def=d"):
             self.assertTrue(require_any("abc", "def"))
     with self.subTest("one options given with is empty"):
         with app.test_request_context("/?abc="):
             self.assertRaises(ValidationFailedException,
                               lambda: require_any("abc"))
     with self.subTest("one options given with is empty but ok"):
         with app.test_request_context("/?abc="):
             self.assertTrue(require_any("abc", empty=True))
Example #10
0
    def test_resolve_auth_token(self):
        with self.subTest("no auth"):
            with app.test_request_context("/"):
                self.assertIsNone(resolve_auth_token())

        with self.subTest("param"):
            with app.test_request_context("/?auth=abc"):
                self.assertEqual(resolve_auth_token(), "abc")

        with self.subTest("bearer token"):
            with app.test_request_context(
                    "/", headers={"Authorization": "Bearer abc"}):
                self.assertEqual(resolve_auth_token(), "abc")

        with self.subTest("basic token"):
            userpass = base64.b64encode(b"epidata:abc").decode("utf-8")
            with app.test_request_context(
                    "/", headers={"Authorization": f"Basic {userpass}"}):
                self.assertEqual(resolve_auth_token(), "abc")
Example #11
0
 def test_is_using_status_codes(self):
     with app.test_request_context('/?format=csv'):
         self.assertTrue(_is_using_status_codes())
     with app.test_request_context('/?format=json'):
         self.assertTrue(_is_using_status_codes())
     with app.test_request_context('/?format=jsonl'):
         self.assertTrue(_is_using_status_codes())
     with app.test_request_context('/'):
         self.assertFalse(_is_using_status_codes())
     with app.test_request_context('/?format=classic'):
         self.assertFalse(_is_using_status_codes())
     with app.test_request_context('/?format=tree'):
         self.assertFalse(_is_using_status_codes())
Example #12
0
 def test_single_parse_source_signal_arg(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertRaises(ValidationFailedException,
                               parse_single_source_signal_arg, "signal")
     with self.subTest("single"):
         with app.test_request_context("/?signal=src1:sig1"):
             self.assertEqual(parse_single_source_signal_arg("signal"),
                              SourceSignalPair("src1", ["sig1"]))
     with self.subTest("single list"):
         with app.test_request_context("/?signal=src1:sig1,sig2"):
             self.assertRaises(ValidationFailedException,
                               parse_single_source_signal_arg, "signal")
     with self.subTest("multi"):
         with app.test_request_context("/?signal=src2:*;src1:*"):
             self.assertRaises(ValidationFailedException,
                               parse_single_source_signal_arg, "signal")
     with self.subTest("wrong"):
         with app.test_request_context("/?signal=abc"):
             self.assertRaises(ValidationFailedException,
                               parse_single_source_signal_arg, "signal")
         with app.test_request_context("/?signal=sig=4"):
             self.assertRaises(ValidationFailedException,
                               parse_single_source_signal_arg, "signal")
Example #13
0
 def test_single_parse_geo_arg(self):
     with self.subTest("empty"):
         with app.test_request_context("/"):
             self.assertRaises(ValidationFailedException,
                               parse_single_geo_arg, "geo")
     with self.subTest("single"):
         with app.test_request_context("/?geo=state:AK"):
             self.assertEqual(parse_single_geo_arg("geo"),
                              GeoPair("state", ["ak"]))
     with self.subTest("single list"):
         with app.test_request_context("/?geo=state:AK,TK"):
             self.assertRaises(ValidationFailedException,
                               parse_single_geo_arg, "geo")
     with self.subTest("multi"):
         with app.test_request_context("/?geo=state:*;nation:*"):
             self.assertRaises(ValidationFailedException,
                               parse_single_geo_arg, "geo")
     with self.subTest("wrong"):
         with app.test_request_context("/?geo=abc"):
             self.assertRaises(ValidationFailedException,
                               parse_single_geo_arg, "geo")
         with app.test_request_context("/?geo=state=4"):
             self.assertRaises(ValidationFailedException,
                               parse_single_geo_arg, "geo")
Example #14
0
    def test_parse_geo_arg(self):
        with self.subTest("empty"):
            with app.test_request_context("/"):
                self.assertEqual(parse_geo_arg(), [])
        with self.subTest("single"):
            with app.test_request_context("/?geo=state:*"):
                self.assertEqual(parse_geo_arg(), [GeoPair("state", True)])
            with app.test_request_context("/?geo=state:AK"):
                self.assertEqual(parse_geo_arg(), [GeoPair("state", ["ak"])])
        with self.subTest("single list"):
            with app.test_request_context("/?geo=state:AK,TK"):
                self.assertEqual(parse_geo_arg(),
                                 [GeoPair("state", ["ak", "tk"])])
        with self.subTest("multi"):
            with app.test_request_context("/?geo=state:*;nation:*"):
                self.assertEqual(
                    parse_geo_arg(),
                    [GeoPair("state", True),
                     GeoPair("nation", True)])
            with app.test_request_context("/?geo=state:AK;nation:US"):
                self.assertEqual(
                    parse_geo_arg(),
                    [GeoPair("state", ["ak"]),
                     GeoPair("nation", ["us"])],
                )
            with app.test_request_context("/?geo=state:AK;state:KY"):
                self.assertEqual(
                    parse_geo_arg(),
                    [GeoPair("state", ["ak"]),
                     GeoPair("state", ["ky"])],
                )
        with self.subTest("multi list"):
            with app.test_request_context(
                    "/?geo=state:AK,TK;county:42003,40556"):
                self.assertEqual(
                    parse_geo_arg(),
                    [
                        GeoPair("state", ["ak", "tk"]),
                        GeoPair("county", ["42003", "40556"]),
                    ],
                )
        with self.subTest("hybrid"):
            with app.test_request_context(
                    "/?geo=nation:*;state:PA;county:42003,42002"):
                self.assertEqual(
                    parse_geo_arg(),
                    [
                        GeoPair("nation", True),
                        GeoPair("state", ["pa"]),
                        GeoPair("county", ["42003", "42002"]),
                    ],
                )

        with self.subTest("wrong"):
            with app.test_request_context("/?geo=abc"):
                self.assertRaises(ValidationFailedException, parse_geo_arg)
            with app.test_request_context("/?geo=state=4"):
                self.assertRaises(ValidationFailedException, parse_geo_arg)
Example #15
0
    def test_extract_dates(self):
        with self.subTest("empty"):
            with app.test_request_context("/"):
                self.assertIsNone(extract_dates("s"))
        with self.subTest("single"):
            with app.test_request_context("/?s=20200101"):
                self.assertEqual(extract_dates("s"), [20200101])
        with self.subTest("multiple"):
            with app.test_request_context("/?s=20200101,20200102"):
                self.assertEqual(extract_dates("s"), [20200101, 20200102])
        with self.subTest("multiple param"):
            with app.test_request_context("/?s=20200101&s=20200102"):
                self.assertEqual(extract_dates("s"), [20200101, 20200102])
        with self.subTest("multiple param mixed"):
            with app.test_request_context("/?s=20200101&s=20200102,20200103"):
                self.assertEqual(extract_dates("s"),
                                 [20200101, 20200102, 20200103])
        with self.subTest("single iso"):
            with app.test_request_context("/?s=2020-01-01"):
                self.assertEqual(extract_dates("s"), [20200101])
        with self.subTest("multiple iso"):
            with app.test_request_context("/?s=2020-01-01,2020-01-02"):
                self.assertEqual(extract_dates("s"), [20200101, 20200102])
        with self.subTest("multiple param iso"):
            with app.test_request_context("/?s=2020-01-01&s=2020-01-02"):
                self.assertEqual(extract_dates("s"), [20200101, 20200102])
        with self.subTest("multiple param mixed iso"):
            with app.test_request_context(
                    "/?s=2020-01-01&s=2020-01-02,2020-01-03"):
                self.assertEqual(extract_dates("s"),
                                 [20200101, 20200102, 20200103])

        with self.subTest("not a date"):
            with app.test_request_context("/?s=a"):
                self.assertRaises(ValidationFailedException,
                                  lambda: extract_dates("s"))

        with self.subTest("simple range"):
            with app.test_request_context("/?s=20200101-20200102"):
                self.assertEqual(extract_dates("s"), [(20200101, 20200102)])
        with self.subTest("inverted range"):
            with app.test_request_context("/?s=20200102-20200101"):
                self.assertRaises(ValidationFailedException,
                                  lambda: extract_dates("s"))
        with self.subTest("single range"):
            with app.test_request_context("/?s=20200101-20200101"):
                self.assertEqual(extract_dates("s"), [20200101])

        with self.subTest("simple range iso"):
            with app.test_request_context("/?s=2020-01-01:2020-01-02"):
                self.assertEqual(extract_dates("s"), [(20200101, 20200102)])
        with self.subTest("inverted range iso"):
            with app.test_request_context("/?s=2020-01-02:2020-01-01"):
                self.assertRaises(ValidationFailedException,
                                  lambda: extract_dates("s"))
        with self.subTest("single range iso"):
            with app.test_request_context("/?s=2020-01-01:2020-01-01"):
                self.assertEqual(extract_dates("s"), [20200101])
Example #16
0
    def test_parse_time_arg(self):
        with self.subTest("empty"):
            with app.test_request_context("/"):
                self.assertEqual(parse_time_arg(), [])
        with self.subTest("single"):
            with app.test_request_context("/?time=day:*"):
                self.assertEqual(parse_time_arg(), [TimePair("day", True)])
            with app.test_request_context("/?time=day:20201201"):
                self.assertEqual(parse_time_arg(),
                                 [TimePair("day", [20201201])])
        with self.subTest("single list"):
            with app.test_request_context("/?time=day:20201201,20201202"):
                self.assertEqual(parse_time_arg(),
                                 [TimePair("day", [20201201, 20201202])])
        with self.subTest("single range"):
            with app.test_request_context("/?time=day:20201201-20201204"):
                self.assertEqual(parse_time_arg(),
                                 [TimePair("day", [(20201201, 20201204)])])
        with self.subTest("multi"):
            with app.test_request_context("/?time=day:*;week:*"):
                self.assertEqual(
                    parse_time_arg(),
                    [TimePair("day", True),
                     TimePair("week", True)])
            with app.test_request_context("/?time=day:20201201;week:202012"):
                self.assertEqual(
                    parse_time_arg(),
                    [TimePair("day", [20201201]),
                     TimePair("week", [202012])],
                )
        with self.subTest("hybrid"):
            with app.test_request_context(
                    "/?time=day:*;day:20202012;week:202101-202104"):
                self.assertEqual(
                    parse_time_arg(),
                    [
                        TimePair("day", True),
                        TimePair("day", [20202012]),
                        TimePair("week", [(202101, 202104)]),
                    ],
                )

        with self.subTest("wrong"):
            with app.test_request_context("/?time=abc"):
                self.assertRaises(ValidationFailedException, parse_time_arg)
            with app.test_request_context("/?time=sig=4"):
                self.assertRaises(ValidationFailedException, parse_time_arg)
            with app.test_request_context("/?time=month:201210"):
                self.assertRaises(ValidationFailedException, parse_time_arg)
            with app.test_request_context("/?time=week:20121010"):
                self.assertRaises(ValidationFailedException, parse_time_arg)
Example #17
0
    def test_parse_source_signal_arg(self):
        with self.subTest("empty"):
            with app.test_request_context("/"):
                self.assertEqual(parse_source_signal_arg(), [])
        with self.subTest("single"):
            with app.test_request_context("/?signal=src1:*"):
                self.assertEqual(parse_source_signal_arg(),
                                 [SourceSignalPair("src1", True)])
            with app.test_request_context("/?signal=src1:sig1"):
                self.assertEqual(parse_source_signal_arg(),
                                 [SourceSignalPair("src1", ["sig1"])])
        with self.subTest("single list"):
            with app.test_request_context("/?signal=src1:sig1,sig2"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [SourceSignalPair("src1", ["sig1", "sig2"])],
                )
        with self.subTest("multi"):
            with app.test_request_context("/?signal=src1:*;src2:*"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [
                        SourceSignalPair("src1", True),
                        SourceSignalPair("src2", True)
                    ],
                )
            with app.test_request_context("/?signal=src1:sig1;src2:sig3"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [
                        SourceSignalPair("src1", ["sig1"]),
                        SourceSignalPair("src2", ["sig3"]),
                    ],
                )
            with app.test_request_context("/?signal=src1:sig1;src1:sig4"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [
                        SourceSignalPair("src1", ["sig1"]),
                        SourceSignalPair("src1", ["sig4"]),
                    ],
                )
        with self.subTest("multi list"):
            with app.test_request_context(
                    "/?signal=src1:sig1,sig2;county:sig5,sig6"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [
                        SourceSignalPair("src1", ["sig1", "sig2"]),
                        SourceSignalPair("county", ["sig5", "sig6"]),
                    ],
                )
        with self.subTest("hybrid"):
            with app.test_request_context(
                    "/?signal=src2:*;src1:sig4;src3:sig5,sig6"):
                self.assertEqual(
                    parse_source_signal_arg(),
                    [
                        SourceSignalPair("src2", True),
                        SourceSignalPair("src1", ["sig4"]),
                        SourceSignalPair("src3", ["sig5", "sig6"]),
                    ],
                )

        with self.subTest("wrong"):
            with app.test_request_context("/?signal=abc"):
                self.assertRaises(ValidationFailedException,
                                  parse_source_signal_arg)
            with app.test_request_context("/?signal=sig=4"):
                self.assertRaises(ValidationFailedException,
                                  parse_source_signal_arg)