def test_post_method_with_bill_to(self):
     """Tests POST reuest method without any time specified"""
     request = AtlasCreateRequest(**{
         "key": "path_to_key",
         "measurements": [self.measurement],
         "sources": [self.create_source],
         "bill_to": "billing@address"
     })
     self.maxDiff = None
     expected_args = {
         "json": {
             "definitions": [{
                 "af": 6, "description": "testing",
                 "target": "testing", "type": "ping"
             }],
             "is_oneoff": False,
             "probes": [{"requested": 3, "type": "area", "value": "WW"}],
             "bill_to": "billing@address",
         },
         "params": {"key": "path_to_key"},
         "verify": True,
         "headers": {
             "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
             "Content-Type": "application/json",
             "Accept": "application/json"
         },
         "proxies": {},
     }
     with mock.patch("ripe.atlas.cousteau.request.AtlasRequest.http_method") as mock_get:
         request._construct_post_data()
         mock_get.return_value = True
         request.post()
         self.assertEqual(request.http_method_args, expected_args)
class TestAtlasCreateRequest(TestCase):
    def setUp(self):
        self.create_source = AtlasSource(
            **{"type": "area", "value": "WW", "requested": 3}
        )
        self.measurement = Ping(**{
            "target": "testing", "af": 6,
            "description": "testing"
        })
        self.request = AtlasCreateRequest(**{
            "start_time": datetime(2015, 10, 16),
            "stop_time": 1445040000,
            "key": "path_to_key",
            "measurements": [self.measurement],
            "sources": [self.create_source],
            "is_oneoff": True,
        })

    def test_construct_post_data(self):
        """Tests construction of past data"""
        self.request._construct_post_data()
        validate(self.request.post_data, post_data_create_schema)

    def test_post_method(self):
        """Tests POST reuest method"""
        self.maxDiff = None
        expected_args = {
            "json": {
                "definitions": [{
                    "af": 6, "description": "testing",
                    "target": "testing", "type": "ping"
                }],
                "is_oneoff": True,
                "probes": [{"requested": 3, "type": "area", "value": "WW"}],
                "start_time": 1444953600,
                "stop_time": 1445040000
            },
            "params": {"key": "path_to_key"},
            "verify": True,
            "headers": {
                "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "proxies": {},
        }
        with mock.patch("ripe.atlas.cousteau.request.AtlasRequest.http_method") as mock_get:
            self.request._construct_post_data()
            mock_get.return_value = True
            self.request.post()
            self.assertEqual(self.request.http_method_args, expected_args)

    def test_post_method_without_times(self):
        """Tests POST reuest method with a bill to address specified"""
        request = AtlasCreateRequest(**{
            "key": "path_to_key",
            "measurements": [self.measurement],
            "sources": [self.create_source],
        })
        self.maxDiff = None
        expected_args = {
            "json": {
                "definitions": [{
                    "af": 6, "description": "testing",
                    "target": "testing", "type": "ping"
                }],
                "is_oneoff": False,
                "probes": [{"requested": 3, "type": "area", "value": "WW"}],
            },
            "params": {"key": "path_to_key"},
            "verify": True,
            "headers": {
                "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "proxies": {},
        }
        with mock.patch("ripe.atlas.cousteau.request.AtlasRequest.http_method") as mock_get:
            request._construct_post_data()
            mock_get.return_value = True
            request.post()
            self.assertEqual(request.http_method_args, expected_args)

    def test_post_method_with_bill_to(self):
        """Tests POST reuest method without any time specified"""
        request = AtlasCreateRequest(**{
            "key": "path_to_key",
            "measurements": [self.measurement],
            "sources": [self.create_source],
            "bill_to": "billing@address"
        })
        self.maxDiff = None
        expected_args = {
            "json": {
                "definitions": [{
                    "af": 6, "description": "testing",
                    "target": "testing", "type": "ping"
                }],
                "is_oneoff": False,
                "probes": [{"requested": 3, "type": "area", "value": "WW"}],
                "bill_to": "billing@address",
            },
            "params": {"key": "path_to_key"},
            "verify": True,
            "headers": {
                "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "proxies": {},
        }
        with mock.patch("ripe.atlas.cousteau.request.AtlasRequest.http_method") as mock_get:
            request._construct_post_data()
            mock_get.return_value = True
            request.post()
            self.assertEqual(request.http_method_args, expected_args)