How to test Facebook login with Ember + Torii
Configuring Torii is super simple. The only trick is that the name of the session conflicts with
ember-simple-auth. I renamed mine to
this.get('torii').open('facebook-oauth2') triggers a Facbook dialog to show in another window. There may be a better way to get the token into simple auth but I wrote a synchronous method that calls some private APIs... don't judge me.
Logging into Facebook triggers a
postMessage to notify Torii of success or failure. Torii then
POSTs the code to your backend using the adapter.
The adapter is simple enough to mock with
ember-cli-mirage. I just return a token if the parameters exist.
The more challenging part (usually) is stubbing the Facebook authentication. Here is what I wanted to write:
ember-cli-page-object make the tests read so pretty!
Not surprisingly, Ember's dependency injection and some great patterns in Torii make swapping the original
facebook-oauth2 service for our stubbed service straight forward.
There were only slight changes needed to test Facebook auth failure. Similarly, adding Google or Twitter authentication won't require many changes.Tweet