def test_catch_all(self):
        self.client.stub(
            request(),
            response()
        )

        result = requests.get(MOCK_SERVER_URL + "/whatever")
        self.assertEqual(result.status_code, 200)
Exemple #2
0
    def get_authn_cookies(self, email):
        self.client.stub(request(method="GET", path="/o/oauth2/auth"),
                         response(code=200, body="fake google auth"))

        url = f"{self.base}/login"
        r = requests.get(url)
        assert r.status_code == 200

        # Retrieve the state that our app sent to the mock server when it redirected
        # to the google oauth2 auth endpoint.
        # http://www.mock-server.com/mock_server/mockserver_clients.html#rest-api shows
        # the mock-server endpoint that can retrieve requests it received.
        # Requested this feature be added to the mock-server python client here
        # https://github.com/internap/python-mockserver-client/issues/16.
        data = json.dumps({"path": "/o/oauth2/auth", "method": "GET"})
        r = requests.put("http://mockserver:1080/retrieve?type=REQUESTS",
                         data=data)
        assert 200 == r.status_code
        state = r.json()[-1]["queryStringParameters"]["state"][0]
        assert "" != state

        # Mock out the app attempting to get an access token using the state and code from Google
        tokenJson = json.dumps({
            "access_token": "myfakeaccesstoken",
            "token_type": "Bearer",
            "expires_in": 0,
        })
        self.client.stub(
            request(method="POST", path="/o/oauth2/token"),
            response(code=200,
                     body=tokenJson,
                     headers={"Content-Type": "application/json"}))

        # Mock out the app attempting to get profile data using the access token
        data = json.dumps({"email": email})
        self.client.stub(request(method="GET", path="/oauth2/v2/userinfo"),
                         response(code=200, body=data))

        # Perform the callback from Google to finish the authentication with the app, return the cookie
        code = "supersecretcode"
        r = requests.get(f"{self.base}/callback?state={state}&code={code}")
        assert 204 == r.status_code
        cookie = r.cookies['dndtextapisession']
        return dict(dndtextapisession=cookie)
Exemple #3
0
    def test_expect_atleast_once(self):
        self.client.expect(request(), response(), timesrange(atleast=1))

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)

        self.client.verify_expectations()
Exemple #4
0
    def test_headers_stubbing(self):
        self.client.stub(request(headers={"i-am-special": "yeah you are"}),
                         response(code=200))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.status_code, 404)

        result = requests.get(MOCK_SERVER_URL,
                              headers={"i-am-special": "yeah you are"})
        self.assertEqual(result.status_code, 200)
Exemple #5
0
    def test_cookies_stubbing(self):
        self.client.stub(request(cookies={"i-am-cookie": "sweet-cookie"}),
                         response(code=200))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.status_code, 404)

        result = requests.get(MOCK_SERVER_URL,
                              cookies={"i-am-cookie": "sweet-cookie"})
        self.assertEqual(result.status_code, 200)
Exemple #6
0
    def test_querystring_stubbing(self):
        self.client.stub(request(querystring={
            "a": "b",
            "c[0]": "d"
        }), response(code=200))

        result = requests.get(MOCK_SERVER_URL + "/?e=f")
        self.assertEqual(result.status_code, 404)

        result = requests.get(MOCK_SERVER_URL + "/?a=b&c[0]=d")
        self.assertEqual(result.status_code, 200)
    def test_default_count_for_stubs_is_unlimited(self):
        self.client.stub(
            request(),
            response()
        )

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)
Exemple #8
0
    def test_expect_atmost_once_fails(self):
        self.client.expect(request(), response(), timesrange(atmost=1))

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 404)

        with self.assertRaises(AssertionError):
            self.client.verify_expectations()
    def test_body_stubbing(self):
        self.client.stub(
            request(body="hey there"),
            response(code=200)
        )

        result = requests.post(MOCK_SERVER_URL, data="sup?")
        self.assertEqual(result.status_code, 404)

        result = requests.post(MOCK_SERVER_URL, data="hey there")
        self.assertEqual(result.status_code, 200)
    def test_method_stubbing(self):
        self.client.stub(
            request(method="POST"),
            response(code=200)
        )

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.status_code, 404)

        result = requests.post(MOCK_SERVER_URL)
        self.assertEqual(result.status_code, 200)
    def test_form_request(self):
        self.client.stub(request(body=form({
            "a": "b",
            "c[0]": "d"
        })), response())

        result = requests.post(MOCK_SERVER_URL, data={"a": "b"})
        self.assertEqual(result.status_code, 404)

        result = requests.post(MOCK_SERVER_URL, data={"a": "b", "c[0]": "d"})
        self.assertEqual(result.status_code, 200)
    def test_path_stubbing(self):
        self.client.stub(
            request(path="/path"),
            response(code=200)
        )

        result = requests.get(MOCK_SERVER_URL + "/whatrver")
        self.assertEqual(result.status_code, 404)

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)
    def test_count_stubbing(self):
        self.client.stub(
            request(),
            response(),
            times(1)
        )

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 200)

        result = requests.get(MOCK_SERVER_URL + "/path")
        self.assertEqual(result.status_code, 404)
    def test_expect_json_contains(self):
        self.client.expect(request(body=json_contains({"key": "value"})),
                           response(), times(1))

        result = requests.get(MOCK_SERVER_URL + "/path",
                              json={"key1": "value2"})
        self.assertEquals(result.status_code, 404)

        result = requests.get(MOCK_SERVER_URL + "/path",
                              json={
                                  "key1": "value2",
                                  "key": "value"
                              })
        self.assertEquals(result.status_code, 200)
Exemple #15
0
 def test_http_requests(self):
     _client = mk.MockServerClient("http://localhost:1080")
     _client.expect(mk.request("GET", "/user"),
                    mk.response(200, "I'm here"), mk.times(2))
 def test_verify_request_received_once(self):
     self.client.stub(request(), response())
     requests.get(MOCK_SERVER_URL)
     self.client.verify(request(), times(1))
 def test_verify_all_expectations(self):
     self.client.expect(request(), response(), times(1))
     requests.get(MOCK_SERVER_URL)
     self.client.verify_expectations()
Exemple #18
0
    def test_code_response(self):
        self.client.stub(request(), response(code=418))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.status_code, 418)
Exemple #19
0
    def test_headers_response(self):
        self.client.stub(request(), response(headers={"i-like": "i-like"}))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.headers["i-like"], "i-like")
Exemple #20
0
    def test_body_response(self):
        self.client.stub(request(), response(body="hey there"))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.content.decode(), "hey there")
Exemple #21
0
 def test_expect_with_ttl(self):
     self.client.expect(request(), response(), times(1), seconds(10))
     result = requests.get(MOCK_SERVER_URL + "/path")
     self.assertEqual(result.status_code, 200)
Exemple #22
0
    def test_reset_should_clear_expectations(self):
        self.client.expect(request(), response(), times(1))

        self.client.reset()
        self.client.verify_expectations()
Exemple #23
0
    def test_expect_once_not_called_fails(self):
        self.client.expect(request(), response(), times(1))

        with self.assertRaises(AssertionError):
            self.client.verify_expectations()
Exemple #24
0
    def test_expect_never(self):
        self.client.expect(request(), response(), times(0))

        self.client.verify_expectations()
Exemple #25
0
    def test_cookies_response(self):
        self.client.stub(request(),
                         response(cookies={"i-am-cookie": "sweet-cookie"}))

        result = requests.get(MOCK_SERVER_URL)
        self.assertEqual(result.cookies["i-am-cookie"], "sweet-cookie")