This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remote Nimbus User creation (local functionality),
mostly working by way of their correspoding Unit tests.
- Loading branch information
1 parent
0e13f00
commit 0014e6d
Showing
8 changed files
with
99 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
TODO: should this file go here? | |||
|
|||
|
|||
Running tests | |||
------------- | |||
|
|||
Example: test only the "nimbus" app test: | |||
$ python nimbusweb/portal/manage.py test nimbus --pythonpath="." |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
import sys | |||
from django.conf import settings | |||
from nimbusrest.connection import Connection | |||
import models | |||
|
|||
def nimbus_user_create_remote(user_instance, path="/users/create", nimbus_key=None, nimbus_secret=None): | |||
"""Use the Nimbus API to register a new Nimbus User. | |||
`user_instance` is a `Django User` instance. | |||
""" | |||
service_uri = getattr(settings, "NIMBUS_SERVICE_URI", "http://some.sensible.default/") | |||
conn = Connection(service_uri, nimbus_key, nimbus_secret) | |||
body = {"username":user_instance.username} #XXX what else is needed? | |||
response = conn.post_json(path, body) #let caller handle errors | |||
return response | |||
|
|||
def nimbus_user_create(sender, instance, **kwargs): | |||
"""Django User model `post_save` function. | |||
Save response data from a successful `Nimbus User` | |||
creation attempt, or, in the case of failure, remove | |||
the recently created `Django User` and send failure | |||
message back to User create Form. | |||
""" | |||
print "complete_nimbus_user_create => ", sender, instance, kwargs | |||
remote_user_creator = kwargs.get("remote_user_creator") | |||
if remote_user_creator is None: | |||
remote_user_creator = nimbus_user_create_remote | |||
# only attempt to create Nimbus User on Django User creation. | |||
if kwargs.get('created'): | |||
response = remote_user_creator(instance) | |||
try: | |||
response = remote_user_creator(instance) | |||
except: | |||
#Nimbus User failed to be created, delete Django User | |||
instance.delete() #XXX how to handle this? | |||
raise Exception(sys.exc_type) | |||
up = models.UserProfile.objects.get(user=instance) | |||
up.nimbus_userid = response["nimbus_userid"] | |||
up.save() | |||
return True | |||
|
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,23 +1,43 @@ | |||
""" | import unittest | ||
This file demonstrates two different styles of tests (one doctest and one | from django.test import TestCase | ||
unittest). These will both pass when you run "manage.py test". | |||
|
|
||
Replace these with more appropriate tests for your application. | from django.contrib.auth.models import User | ||
""" | |||
|
|
||
from django.test import TestCase | import remote | ||
|
|
||
class SimpleTest(TestCase): | class FakeUser(object): | ||
def test_basic_addition(self): | username = "test_username" | ||
""" | email = "test@example.com" | ||
Tests that 1 + 1 always equals 2. | password = "test_password" | ||
|
|||
class CreateNimbusUserTest(unittest.TestCase): | |||
|
|||
def setUp(self): | |||
#self.user = User.objects.create_user("test_username", "test@email.com", "test_password") | |||
#creating a real user kicks off a real 'models.signals.post_save.connect'! So do this: | |||
self.user = FakeUser() | |||
self.ok_resp = {"nimbus_userid":"abc123"} #, "state":"created"} | |||
|
|||
def _fake_remote_user_creator(self, user_instance, fail=False): | |||
"""Creator real request, fake response and failure states. | |||
Get necessary data from `user_instance` to form a correct | |||
request to create a remote Nimbus User. | |||
""" | """ | ||
self.failUnlessEqual(1 + 1, 2) | if not fail: | ||
return self.ok_resp | |||
else: | |||
raise Exception("fake error in '_fake_remote_user_creator'") | |||
|
|
||
__test__ = {"doctest": """ | def test_create_user(self): | ||
Another way to test that 1 + 1 is equal to 2. | """ | ||
""" | |||
passthrough = lambda x:x | |||
created = remote.nimbus_user_create(passthrough, self.user, created=True, | |||
remote_user_creator=self._fake_remote_user_creator) | |||
self.assertEquals(created, True) | |||
|
|
||
>>> 1 + 1 == 2 | def test_create_user_failed(self): | ||
True | #test that User rollback is successful. | ||
"""} | pass | ||
|
|
This file contains 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