How do we make Trek's Pretender play nice with Ember Testing? - ember.js

So, my current Ember project is built using Ember App Kit. My tests are using the wonderful httpRespond to mock out ajax requests.
However, I have started to notice that while httpRespond is great, you really only test how your app responds to responses from the API and not so much how your app responds to interactions from the user. An example of this I guess is submitting a form with server side field validations.
With httpRespond you mock out the response, which will be returned regardless of what the request looked like. So, I can essentially click the submit button on my form and successfully submit the form without having filled in any fields. This feels like we're missing something.
Enter Trek's Pretender. This is a bit like a sup'ed up version of httpRespond. It looks a little like a mock server but is just mocking out the xhr like httpRespond. Except you get access to the request which you can inspect before deciding what response to return.
I like this idea a lot and I want to use it. However....
Pretender is not yet Ember Testing aware. httpRepond understands the async workings of Ember and will wait for async events in Ember to finish before carrying on in the test. Pretender however, does not do this yet.
For instance, if I click a link in my Ember app which kicks off a few different async events, my test will not wait for these async events to finish before continuing and therefore, the test finishes executing before the async events have finished.
Which brings me to my question...
How do we go about making Pretender Ember Testing aware?
Trek has mentioned that this is something he has yet to do, but I'm not sure when he might have time to get to it. So I'd love to get it going if possible.
Does anyone have any thoughts how we might about attempting this?

I'm having a lot of success with ember-cli-mirage. It sits on top of pretender and allows you to create both fixtures for development and factories for use in tests. If you're still having trouble with this, or for anyone else, this is a really easy way to get control over your apps development data.

Related

How to unit test authorization globally in CakePHP

We're using CakePHP v3.1.x with the CakeDC Users plugin.
We're trying to set up our unit tests to help prevent accidentally allowing non-admins to do things they should not be allowed to do. For example, imagine that a developer creates a new admin feature with a new action in a controller. During development she sets the permissions very lax so she doesn't have to log in each time to test it (or something... you get the idea). I'm trying to write a test that will fail if she tries to push this...
Here's my idea for how to do it:
Create a test that loops over all controllers / actions in the app, and checks to see if a non-admin user is authorized.
The test has a list of every action that a non-admin user can do.
The test fails if a non-admin is allowed to do anything that's not on the list.
The idea is that every time we intentionally let non-admin users do something, the test fails, and reminds us to go update the list of exceptions. But if we accidentally allow an action without knowing it, the test fails and we fix the mistake. It's a safety catch.
My question: Is this the right way to do this? And if so, how can we dynamically generate a list of all of the apps controllers/actions?
Is this the right way to do this?
Fix the problem not the symptoms: Fix your developers behavior of committing "test" and "debug" code or whatever you want to name it. Do peer reviews or have your team leader review the commits. You could try to ask on https://workplace.stackexchange.com/ about how to deal with this situation the best. Check this question for an example.
And if so, how can we dynamically generate a list of all of the apps controllers/actions?
Use this to get a list of folders with controllers:
App::path('Controller'); // App
App::path('Controller', 'MyPlugin'); // Some plugin
Use Plugin::loaded() to get a list of all plugins.
Then use the information to read the controller classes from all the folders. You can then generate test cases (maybe trough a new task of the bake shell?) automatically because you've got a list of controllers from your app and plugin. Use reflections to get a list of public methods from the controllers and filter known public methods like initialize().
Setting up testing auth is described here.
If you write a custom test that uses an array structure describing the controllers and actions and their permissions I'm pretty sure that you'll be able to automate it by running over that array structure and using the information to run the auth tests. But honestly, I'm not going to do that work here.
Or instead of testing the code directly use Codeception acceptance testing. CakePHP has plugin for that. This will allow you to test the "real" site by having automated click-through testing. It should be pretty easy to set up a specific users session and then test against URLs for access by expecting a redirect to the login action or whatever you do.

How to disable Request Method: OPTIONS with ember app kit

Since i'm using EAK, each time I save a model, I have an extra request method. It's more annoying then anything and I'd like to disable it. I haven't found anything in the documentation regarding this. Has anyone ever disabled it or am I stuck with it?
I may be wrong here, but this doesn't sound like a problem with Ember-App-Kit. Are you by any chance using CORS to send your requests? You say that it sends an OPTIONS request when you save a model, but you didn't mention that it sends one when it fetches a model. This makes me think that it's a preflighted request that is unavoidable if you're using CORS. We had this same issue about a month ago and we decided to ditch CORS in favor of a proxy server.
I've never used Ember-App-Kit, but it's just a build tool, so I can't imagine how it would affect your Ember-Data adapter.

websockets with ember js

I'm having some difficulty figuring out how to integrate websockets with my ember js app. I don't know how to emulate websocket behavior in jsbin, so here's the nonfunctional example --
http://jsbin.com/iFUZoza/1/edit
Imagine this working up to the point where I log "finding..." (It does. I get the object I'm looking for from the socket.) Unfortunately from there, I'm not sure how to get the contact model in question out of Ember. Basically, I've listed all of the contacts in the UI, and I want to retrieve a single model from the controller and update it in real time. I've got as far as getting the sockets functional, but I don't know how to get that model back. I'm not even sure if I've inserted the socket code at the right place to do this. Can someone help or point me in the right direction?
Thanks.
I believe the route its a good place to create the socket connection and the controllers are the place to react according to what happened in the socket.
I'd do something that resembles this JSBin:
http://jsbin.com/iFUZoza/4/edit?js,output
Hope this helps you!

Where/how to create a background function/thread in Ember.js?

I would like to run a function in the background of an Ember App. I am also using Ember Data. Does Ember.js have a particular way to create a background function/thread?
Say I have a function with a while(true) that runs forever doing the things I would like it to do. Where should I put that function in an Ember App?
Update: Here are some ways (if clickable, you can to see an example in jsbin):
Use Ember.run.schedule on Ember.run.later. See this post and the api.
Using a simple setInterval() in ApplicationRoute. The UI seems to be running smoothly.
Use setInterval() inside a Web Worker.
How is Ember.run.schedule or later better than setInterval?

How to simulate a HTTP Post request from a django view without a template

I am writing views, not so keen to write templates right away.
But I need to test my program by submitting post requests.
How do i simulate HTTP Post from within a django view
I am aware that urllib2 and httplib modules of python allow a lot of options, but I am looking for something that elegantly integrates into the django views.
Would U create a method that performs post, where would you call it from?
Update: Some of the answers deal with testing by sending a POST to my application. What if I want to POST to an external service and deal with POST response. How can I do it without writing templates.
Django has a built in mock Client utility that can mimic requests as if they are coming from a browser. If you don't need to fully mimic a browser and just want to invoke your views directly from your tests, consider using a RequestFactory instead.
For such cases I think RequestFactory is ideally suited.
It works just like django's test client with the difference that it let's you create a request object that you can use anywhere. So you could just create your own request object and pass it to your view or form for testing.
I like this method of testing more then using the test client, since it comes closer to pure unit testing. That is, testing a single piece of code. If you're using the test client, there are more layers added before the actual code you're testing is reached.
To avoid the pain of creating the request object yourself you can use this tip on Django snippets
It sounds like you are looking for either a unit test or an acceptance test. Take a look at unittest which is part of the standard library.
For quick ad hoc tests while developing web apps, I like to use curl. It's a simple command line tool that easily generates all sorts of HTTP requests. You can POST with a command like:
curl -i -d field=value http://localhost:8080/sample/something
Curl is available on a lot of platforms. Check it out at http://curl.haxx.se/
If you are looking at this from the context of writing unittests, you could consider creating the Request object yourself and just calling the view function directly. You could even mock it, and any other parameters the view might take.