Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
added fxauth - related to #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekziade committed Apr 20, 2017
1 parent 218bf09 commit 58ed4bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
36 changes: 36 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,39 @@ Mozlotov
========

Mozilla-specific helpers for writing Molotov tests.


FXA
---

In order to use an API that requires a Firefox Account Bearer token,
you can use the **FXATestAccount** class. The class will create
a test user for you and provide a Bearer token.

Example of Molotov integration:

.. code-block:: python
from molotov import global_setup, global_teardown setup
from mozlotov import FXATestAccount
_FXA = []
@global_setup()
def create_accoun(args):
# creates the user and get a Bearer token
acct = FXATestAccount()
acct.create()
_FXA.append(acct)
@global_teardown()
def destroy_account():
# destroys the user
_FXA[0].cleanup()
@setup()
async def init_test(worker_id, args):
# grab the Bearer token for the Molotov test
headers = {"Authorization": _FXA[0].authorization()}
return {'headers': headers}
6 changes: 5 additions & 1 deletion mozlotov/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

__version__ = '0.1'

try:
from mozlotov._fxa import FXATestAccount # NOQA
except ImportError:
pass # NOQA
50 changes: 50 additions & 0 deletions mozlotov/_fxa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import functools.partials

from fxa.__main__ import DEFAULT_CLIENT_ID
from fxa.core import Client
from fxa.tests.utils import TestEmailAccount
from fxa.tools.bearer import get_bearer_token


_FXA_SERVER = "https://api.accounts.firefox.com"
_FXA_OAUTH = "https://oauth.accounts.firefox.com/v1"
_PWD = 'MySecretPassword'


class FXATestAccount(object):
def __init__(self, server=_FXA_SERVER, oauth=_FXA_OAUTH, password=_PWD):
self.server = server
self.oauth = oauth
self.session = self.token = None
self.password = password
self.acct = TestEmailAccount()
self.client = Client(_FXA_SERVER)

def _verify(self, session, response):
code = response["headers"].get('x-verify-code')
if code is None:
return False
session.verify_email_code(code)
return True

def create(self):
session = self.client.create_account(self.acct.email, self.password)
m = self.acct.wait_for_email(functools.partial(self._verify, session))
if m is None:
raise RuntimeError("verification email did not arrive")

self.token = get_bearer_token(self.acct.email, self.password
account_server_url=self.server+"/v1",
oauth_server_url=self.oauth,
scopes=['sync:addon_storage'],
client_id=DEFAULT_CLIENT_ID)

def cleanup(self):
self.acct.clear()
self.client.destroy_account(self.acct.email, self.password)

def authorization(self):
if self.token is None:
raise RuntimeError("You need to call create() first")
return "Bearer %s" % self.token

0 comments on commit 58ed4bd

Please sign in to comment.