Today I Learned

hashrocket A Hashrocket project

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 torii-session.

Configure

Calling 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.

User Login Service

Logging into Facebook triggers a postMessage to notify Torii of success or failure. Torii then POSTs the code to your backend using the adapter.

Torii Adapter

The adapter is simple enough to mock with ember-cli-mirage. I just return a token if the parameters exist.

API stub

The more challenging part (usually) is stubbing the Facebook authentication. Here is what I wanted to write:

Acceptance Test

Notice how 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.

Stubbed OAuth2 Provider

There were only slight changes needed to test Facebook auth failure. Similarly, adding Google or Twitter authentication won't require many changes.

See More #emberjs TILs