Last active
February 20, 2017 02:43
-
-
Save rdegges/9144261 to your computer and use it in GitHub Desktop.
Automatically seed a Stormpath application with user accounts for testing!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
stormpath-seed.py | |
~~~~~~~~~~~~~~~~~ | |
A simple utility which creates a new Stormpath application named 'test' (or | |
uses one already named 'test', if it exists), and creates a number of user | |
accounts in this application. | |
This is useful for various testing scenarios. | |
If you're using Stormpath to handle your user accounts, you might want to | |
run test code against this test application (so you don't mess with | |
production data), or you could use it for a variety of other purposes. | |
What this gets you: | |
- As many real user accounts as you want. The data is provided by | |
http://randomuser.me | |
- A Stormpath application, named 'test', which contains as many user | |
account as you want. | |
- The ability to specify how many accounts are created. | |
Prerequisites | |
------------- | |
Before running this program, you need several dependencies installed. To | |
install all dependencies, you can run: | |
$ pip install -U gevent requests stormpath | |
You also need to set two environment variables, so that this application | |
can access your Stormpath account. Specifically you need to set: | |
STORMPATH_API_KEY_ID and STORMPATH_API_KEY_SECRET | |
You can set these on the command line by running the following command: | |
$ export STORMPATH_API_KEY_ID=xxx | |
$ export STORMPATH_API_KEY_SECRET=xxx | |
NOTE: These credentials are given to you in the form of an | |
'apiKey.properties' file that Stormpath has you download when you first | |
generate your API key. | |
Usage | |
----- | |
Usage is simple. Just run the script, and pass in the amount of users | |
you'd like created: | |
$ python stormpath-seed.py 1000 | |
If you don't specify a number, 100 accounts will be created for you. | |
That's it! Want help, have questions? Send me an email: [email protected] | |
""" | |
from gevent.monkey import patch_all | |
patch_all() | |
from os import environ | |
from sys import argv, exit | |
from uuid import uuid4 | |
from gevent.pool import Pool | |
from requests import get | |
from stormpath.client import Client | |
from stormpath.error import Error as StormpathError | |
##### GLOBALS | |
client = Client( | |
id = environ.get('STORMPATH_API_KEY_ID'), | |
secret = environ.get('STORMPATH_API_KEY_SECRET'), | |
) | |
try: | |
application = client.applications.create({ | |
'name': 'test', | |
'description': 'A test application filled with test user accounts.', | |
}, create_directory=True) | |
print 'Successfully created new Stormpath Application: test' | |
except StormpathError: | |
try: | |
application = client.applications.search('test')[0] | |
print 'Successfully found existing Stormpath Application: test' | |
except Exception, e: | |
print e | |
##### CODE | |
def create_account(): | |
""" | |
Create a new Stormpath Account, grabbing seed data from: | |
http://randomuser.me | |
:rtype: bool | |
:returns: True if a new Account was created, False otherwise. | |
""" | |
finished = False | |
try: | |
resp = get('http://api.randomuser.me') | |
if resp.status_code != 200: | |
raise | |
user = resp.json()['results'][0]['user'] | |
try: | |
account = application.accounts.create({ | |
'given_name': user['name']['first'].title(), | |
'surname': user['name']['last'].title(), | |
'email': user['email'], | |
'password': uuid4().hex[:20].upper() + uuid4().hex[:20], | |
}) | |
print 'Created new user: %s %s <%s>' % ( | |
account.given_name, | |
account.surname, | |
account.email, | |
) | |
finished = True | |
except KeyboardInterrupt: | |
exit(0) | |
except: | |
pass | |
except KeyboardInterrupt: | |
exit(0) | |
except: | |
print 'Skipping account creation... http://randomuser.me appears to be having issues.' | |
return finished | |
if __name__ == '__main__': | |
count = 100 | |
if len(argv) > 1: | |
try: | |
count = int(argv[1]) | |
except: | |
print 'Invalid number specified.' | |
print 'Usage: python stormpath-seed.py [<count>]' | |
exit(1) | |
pool = Pool(5) | |
for i in xrange(count): | |
pool.spawn(create_account) | |
pool.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment