Scala Test : Mock deadbolt authentication - scalatest

While running scala test, how can deadbolt auth be mocked so that the requests goes through?

Related

Blocking / Initialization service with angular.js

My apps are using many web services on the intranet, and url-s for those depend on the server environment.
My apps are hosted on IIS, which adds an HTTP response header like this: Environment: DEV, so every web app knows in which server environment it is running, and thus which intranet servers it must use to call all the services.
Each of my angular apps uses a service that issues a simple GET against the app's own root just to get any response with the environment name in it, and set configuration accordingly.
Question:
How should an angular app implement such a service that would execute as the very first thing in the application, and make sure that while it is getting that first response, nothing in the app tries to execute an HTTP request against other services, or even try to use any configuration provided by my environment service?
Is there a way to implement such a service in angular that could block every other service / factory in the application till it is done initializing itself?
I have many other services in the app, and none of them really know what to do till my environment service has finished its initialization.
UPDATE
Looking at it from another angle.... is it possible to implement such an interceptor in angular that could do the following?:
execute an HTTP request and block the app's execution till it gets a response
make information from the response available throughout the app as a service/factory/config.
Angular lifecycle could be one solution. Using the angular.config() phase you could peek at the headers of the HTTP service.
Create a factory called 'httpInterceptor'
function httpInterceptors(siteConfig, $q, $injector) {
return {
response: function(data, status, headers) {
siteConfig.setEnvironment(headers['Environment']);
return data;
}
};
)
Then in angular.config()
$httpProvider.interceptors.push('httpInterceptor');
If you truly want to block the other option is to use UI router resolve property to block routes loading until the request has been made https://github.com/angular-ui/ui-router/wiki you can add the resolve method to the root state.
Resolve
You can use resolve to provide your controller with content or data that > is custom to the state. resolve is an optional map of dependencies which > should be injected into the controller.
If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired.

Test case for $http request using jasmine

Currently I am writing test cases in jasmine for my stand-alone angularJs app, I need to write test cases for $http requests also, I wrote the tests to match the response from $http request and $httpBackend request, but that is not enough, what else can I verify for a $http request other than response status.
Can format of data returned be verified? and what other stuff can be checked in a test spec?
Thanks!!
Have a look at Spies: http://angular-tips.com/blog/2014/03/introduction-to-unit-test-spies/
Spy on a service method call using jasmine Spies
You can do anything with a request. You can fake all responses (success, error), fire backend requests, which are faked to local json-files and so on.
Read more about jasmine / karma testing | jasmine / karma fake backend.

How to make Sinon Fake server respond to some routes but not all of them

Is it possible to setup Sinon Fake Server to respond to some routes, for example /products but not /orders.
My scenario is: I am not using Sinon Fake Server for unit testing, I want to fake the server in cases where the API is not ready yet for a given area of the application. I want the application to call the real server in all cases, but when it calls /products, I want sinon to respond it for me.
I could add reference to Sinon to the product pages temporarily until I get the real API and then remove it, but ideally I would like to keep all of that isolated from the real implementation. Right now I have a UseFakeServer flag on my main.js file to determine whether sinon is on or not. My fake server has a list of RespondWith, but if a route is not found there I get a 404 instead of trying to go to the real server. fake server AutoRespond is set to true.
Not sure if it makes a difference but I am using backbone on the front end.
Thanks!
Assuming you are doing the API requests via HttpXMLRequest and using at least Sinon 1.3.0 you should use sinon.useFakeXMLHttpRequest and then you are able to filter which requests should be faked.
When using Sinon.JS for mockups or partial integration/functional
testing, you might want to fake some requests, while allowing others
to go throught to the backend server. With filtered
FakeXMLHttpRequests (new in Sinon 1.3.0), you can.
FakeXMLHttpRequest.useFilters Default false. When set to true, Sinon
will check added filters if certain requests should be “unfaked”.
FakeXMLHttpRequest.addFilter(fn) Add a filter that will decide whether
or not to fake a request. The filter will be called when xhr.open is
called, with the exact same arguments (method, url, async, username,
password). If the filter returns true, the request will not be faked.
So for example you want all requests to /products not to be handled by Sinon it would be something like
sinon.FakeXMLHttpRequest.useFilters = true;
sinon.FakeXMLHttpRequest.addFilter(function(method, url, async, username, password) {
return /\/products/g.test(url);
});
See http://sinonjs.org/docs/#server

Karma Jasmine AngularJS testing with real HTTP requests

I want to test my code with real API calls (so I can test the API as well, and when I change the API I don't have to change the JS test as well, and a lot more benefits.) instead of the regular $httpBackend.expectPOST('http://api.com/login').response(200).
Essentially, I want to test a ProductsController that expects to be logged in through an AuthService.login() method and receive a list of products through ui-router's resolve feature.
In this case, the login method receives data that needs to be used to gather products.
From the $httpBackend documentation found here: https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend
As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request (e.g. to fetch templates or static files from the webserver). To configure the backend with this behavior use the passThrough request handler of when instead of respond
So, something like: $httpBackend.whenGET(/.*/).passThrough(); should suffice.

How can I mock the results of the GMail API?

We are using the GMail API and developing an application on top of it. Ideally I would like to have some golden emails to test the analytics engine against. This way I can develop the analytics engine without having to worry about fetching the emails and hence without a network connection. What is the best way to achieve this? I noticed that App Engine (which we use) now allows you to mock DataStore/memcache etc. and run nosetests, but I don't know how to do this across local appserver restarts.
The Mock class provided by googleapis/google-api-python-client looks like a good candidate for your use case.
from googleapiclient.discovery import build
from googleapiclient.http import HttpMock
mock = HttpMock('mock-email-list.json', {'status': '200'})
gmail = build('gmail', 'v1', http=mock)
response = gmail.users().messages().list(userId='me').execute()
print(response)
Where mock-email-list.json content is of the form
{
"messages":[
{
"id":"abcd",
"threadId":"abcd"
},
{
"id":"efgh",
"threadId":"abcd"
},
],
"resultSizeEstimate":2
}
As a side note, after your test has run, you can also check what url the mock has been used for (by the gmail client):
assert mock.uri == 'https://gmail.googleapis.com/gmail/v1/users/me/messages?alt=json'
The Gmail API nor the client libraries provide special functionality for mocking email fetching. You'll need to build out the mocks on your own.

Resources