Exemplo n.º 1
0
 def test_call_makes_request_with_required_parameters(self):
     """
     Test that the 'call' method makes a session request with required params and defaults for
     optional parameters.
     """
     base.call("GET", self.url, self.req_ctx)
     self.session.request.assert_called_once_with("GET", self.url, auth=None, **self.OPTIONAL_REQUEST_ARGS)
Exemplo n.º 2
0
 def test_call_makes_request_with_optional_request_params(self):
     """
     Test that 'call' function makes session request with optional parameter
     set.
     """
     custom_kwargs = self.OPTIONAL_REQUEST_ARGS.copy()
     custom_kwargs.update({
         'params': {
             'foo': 'param'
         },
         'data': {
             'foo': 'data'
         },
         'headers': {
             'bar': 'header'
         },
         'cookies': {
             'oreo': 'cookie'
         },
         'timeout': 60,
         'proxies': {
             'custom': 'proxy'
         },
         'verify': False,
         'cert': ('custom', 'cert'),
         'allow_redirects': False,
     })
     base.call("GET", self.url, self.req_ctx, **custom_kwargs)
     self.session.request.assert_called_once_with("GET",
                                                  self.url,
                                                  auth=None,
                                                  **custom_kwargs)
Exemplo n.º 3
0
 def test_call_makes_request_with_required_parameters(self):
     """
     Test that the 'call' method makes a session request with required params
     and defaults for optional parameters.
     """
     base.call("GET", self.url, self.req_ctx)
     self.session.request.assert_called_once_with(
         "GET", self.url, auth=None, **self.OPTIONAL_REQUEST_ARGS)
Exemplo n.º 4
0
 def test_call_makes_request_with_auth_token(self, authentication_bearer_mock):
     """
     Test that the 'call' method uses custom authentication bearer callable when making request
     """
     auth_token = "my-oauth2-token"
     base.call("GET", self.url, self.req_ctx, auth_token=auth_token)
     # Make sure custom auth class was set up properly
     authentication_bearer_mock.assert_called_once_with(auth_token)
     self.session.request.assert_called_once_with(
         "GET", self.url, auth=authentication_bearer_mock.return_value, **self.OPTIONAL_REQUEST_ARGS
     )
Exemplo n.º 5
0
    def make_retry_call_with_error_code(self, http_error_code, max_retries=None, error_json=None):
        """
        Makes a call that will raise an http error in order to potentially trigger
        the request being retried up to "max_retries" times.  Otherwise, a CanvasAPIError is expected
        to be raised by the underlying call method.  Return the exception in order to do any additional
        assertions.
        """
        self.session.request.return_value.raise_for_status.side_effect = HTTPError(
            response=mock.MagicMock(status_code=http_error_code, json=mock.Mock(return_value=error_json or {}))
        )
        with self.assertRaises(CanvasAPIError) as canvas_error:
            base.call("GET", self.url, self.req_ctx, max_retries=max_retries)

        return canvas_error.exception
Exemplo n.º 6
0
 def test_call_makes_request_with_auth_token(self,
                                             authentication_bearer_mock):
     """
     Test that the 'call' method uses custom authentication bearer callable
     when making request.
     """
     auth_token = "my-oauth2-token"
     base.call("GET", self.url, self.req_ctx, auth_token=auth_token)
     # Make sure custom auth class was set up properly
     authentication_bearer_mock.assert_called_once_with(auth_token)
     self.session.request.assert_called_once_with(
         "GET",
         self.url,
         auth=authentication_bearer_mock.return_value,
         **self.OPTIONAL_REQUEST_ARGS)
Exemplo n.º 7
0
 def test_call_returns_value_of_session_request(self):
     """
     Test that 'call' method returns the result of session request
     """
     result = base.call("GET", self.url, self.req_ctx)
     self.assertEqual(result, self.session.request.return_value,
                      "Session request should be returned")
Exemplo n.º 8
0
 def test_call_returns_value_of_session_request(self):
     """
     Test that 'call' method returns the result of session request
     """
     result = base.call("GET", self.url, self.req_ctx)
     self.assertEqual(
         result, self.session.request.return_value,
         "Session request should be returned")
Exemplo n.º 9
0
 def test_call_makes_request_with_optional_request_params(self):
     """
     Test that 'call' function makes session request with optional parameter set
     """
     custom_kwargs = self.OPTIONAL_REQUEST_ARGS.copy()
     custom_kwargs.update(
         {
             "params": {"foo": "param"},
             "data": {"foo": "data"},
             "headers": {"bar": "header"},
             "cookies": {"oreo": "cookie"},
             "timeout": 60,
             "proxies": {"custom": "proxy"},
             "verify": False,
             "cert": ("custom", "cert"),
             "allow_redirects": False,
         }
     )
     base.call("GET", self.url, self.req_ctx, **custom_kwargs)
     self.session.request.assert_called_once_with("GET", self.url, auth=None, **custom_kwargs)
Exemplo n.º 10
0
 def test_call_makes_request_with_optional_request_params(self):
     """
     Test that 'call' function makes session request with optional parameter
     set.
     """
     custom_kwargs = self.OPTIONAL_REQUEST_ARGS.copy()
     custom_kwargs.update({
         'params': {'foo': 'param'},
         'data': {'foo': 'data'},
         'headers': {'bar': 'header'},
         'cookies': {'oreo': 'cookie'},
         'timeout': 60,
         'proxies': {'custom': 'proxy'},
         'verify': False,
         'cert': ('custom', 'cert'),
         'allow_redirects': False,
     })
     base.call("GET", self.url, self.req_ctx, **custom_kwargs)
     self.session.request.assert_called_once_with(
         "GET", self.url, auth=None, **custom_kwargs)
Exemplo n.º 11
0
    def make_retry_call_with_error_code(self,
                                        http_error_code,
                                        max_retries=None,
                                        error_json=None,
                                        response_headers=None):
        """
        Makes a call that will raise an http error in order to potentially
        trigger the request being retried up to "max_retries" times.  Otherwise,
        an SDKException is expected to be raised by the underlying call method.
        Return the exception in order to do any additional assertions.
        """
        self.session.request.return_value.raise_for_status.side_effect = HTTPError(
        )
        self.session.request.return_value.status_code = http_error_code
        self.session.request.return_value.json.return_value = error_json or {}
        # Response headers
        self.session.request.return_value.headers = response_headers or {}

        with self.assertRaises(SDKException) as canvas_error:
            base.call("GET", self.url, self.req_ctx, max_retries=max_retries)

        return canvas_error.exception
def fetch_syllabus(id):
	'''Fetches syllabus for course with given id from API. Returns syllabus as HTML string'''
	if oauthtoken is None:
		raise_exception()

	# Create request context
	req_context = request_context.RequestContext(oauthtoken, baseurl)

	# Make API request
	r = base.call("GET", baseurl + "/v1/courses/" + str(id), req_context, params={'include[]': 'syllabus_body'}, auth_token=oauthtoken)

	# Decode JSON
	syllabus = r.json()

	return syllabus['syllabus_body']
Exemplo n.º 13
0
def fetch_syllabus(id):
    '''Fetches syllabus for course with given id from API. Returns syllabus as HTML string'''
    if oauthtoken is None:
        raise_exception()

    # Create request context
    req_context = request_context.RequestContext(oauthtoken, baseurl)

    # Make API request
    r = base.call("GET",
                  baseurl + "/v1/courses/" + str(id),
                  req_context,
                  params={'include[]': 'syllabus_body'},
                  auth_token=oauthtoken)

    # Decode JSON
    syllabus = r.json()

    return syllabus['syllabus_body']
Exemplo n.º 14
0
 def test_call_raises_status_on_result(self):
     """
     Test that 'call' method makes call to raise_for_status on result object
     """
     result = base.call("GET", self.url, self.req_ctx)
     result.raise_for_status.assert_called_once_with()
Exemplo n.º 15
0
 def test_call_raises_status_on_result(self):
     """
     Test that 'call' method makes call to raise_for_status on result object
     """
     result = base.call("GET", self.url, self.req_ctx)
     result.raise_for_status.assert_called_once_with()