import simplejson
import test_utils

USERNAME = 'test_created_user'
PASSKEY = 'key'
DATABASE = 'test_DB'

def teardown():
  # Authenticate the test user.
  response = test_utils.make_http_request(
      method='POST',
      url='/users/%s'% USERNAME,
      body=('{"head":{"method":"delete","authorization":["%s"]}}'%
        USERNAME),
      headers={
        'User-Agent': 'UA:DCube teardown :: authenticating testuser',
        'Accept': 'application/jsonrequest',
        'Content-Type': 'application/jsonrequest'})
  assert response.status == 200, (
      'HTTP status is %d when authenticating the temporary test user on '
      '/users/%s'% (response.status, USERNAME))
  json = simplejson.loads(response.body)
  assert json['head']['status'] == 401 or json['head']['status'] == 404, \
      ('DCube status is %d when authenticating the temporary test user on '
      '/users/%s'% (json['head']['status'], USERNAME))

  if json['head']['status'] == 404:
    return

  nonce = json['head']['authorization'][1]
  nextnonce = json['head']['authorization'][2]

  username, cnonce, response = test_utils.create_credentials(
      PASSKEY, USERNAME, nonce, nextnonce)

  # The test user removes himself.
  response = test_utils.make_http_request(
      method='POST',
      url='/users/%s'% USERNAME,
      body='{"head":{"method":"delete", "authorization":["%s","%s","%s"]}}'% \
          (username, cnonce, response),
      headers={
        'User-Agent': 'UA:DCube teardown :: delete testuser',
        'Accept': 'application/jsonrequest',
        'Content-Type': 'application/jsonrequest'})
  assert response.status == 200
  json = simplejson.loads(response.body)
  assert json['head']['status'] == 204

  # Authenticate the admin user.
  response = test_utils.make_http_request(
      method='POST',
      url='/databases/%s'% DATABASE,
      body=('{"head":{"method":"delete","authorization":["%s"]}}'%
        test_utils.ADMIN_USERNAME),
      headers={
        'User-Agent': 'UA:DCube teardown :: authenticating admin user',
        'Accept': 'application/jsonrequest',
        'Content-Type': 'application/jsonrequest'})
  assert response.status == 200, (
      'HTTP status is %d when authenticating the temporary test user on '
      '/databases/%s'% (response.status, DATABASE))
  json = simplejson.loads(response.body)
  assert json['head']['status'] == 401 or json['head']['status'] == 404, \
      'status is %d'% json['head']['status']

  if json['head']['status'] == 404:
    return

  nonce = json['head']['authorization'][1]
  nextnonce = json['head']['authorization'][2]

  username, cnonce, response = test_utils.create_credentials(
      test_utils.ADMIN_PASSKEY, test_utils.ADMIN_USERNAME,
      nonce, nextnonce)

  # Remove the test db.
  response = test_utils.make_http_request(
      method='POST',
      url='/databases/%s'% DATABASE,
      body='{"head":{"method":"delete", "authorization":["%s","%s","%s"]}}'% \
          (username, cnonce, response),
      headers={
        'User-Agent': 'UA:DCube teardown :: remove test db.',
        'Accept': 'application/jsonrequest',
        'Content-Type': 'application/jsonrequest'})
  assert response.status == 200
  json = simplejson.loads(response.body)
  assert json['head']['status'] == 204

@@ -1,125 +1,445 @@
-import simplejson%0Aimport test_utils%0A%0AUSERNAME = 'test_created_user'%0APASSKEY = 'key'%0ADATABASE = 'test_DB'%0A%0Adef teardown():
+%22%22%22%0A  tests.teardown%0A  ~~~~~~~~~~~~~~%0A%0A  Utilities used to teardown databases and users that were created for testing.%0A%0A  :copyright: (c) 2010 by The Fireworks Project.%0A  :license: MIT, see LICENSE for more details.%0A%22%22%22%0A%0Aimport simplejson%0Aimport test_utils%0A%0AUSERNAME = 'test_created_user'%0APASSKEY = 'key'%0ADATABASE = 'test_DB'%0ADATABASE_TOO = 'test_DB_too'%0A%0Adef teardown():%0A  %22%22%22Teardown databases and users that were created for testing%0A  %22%22%22
 %0A  #
@@ -3063,24 +3063,32 @@
  Remove the 
+primary 
 test db.%0A  r
@@ -3571,28 +3571,843 @@
 on%5B'head'%5D%5B'status'%5D == 204%0A
+%0A  # Re-authenticate.%0A  nonce = json%5B'head'%5D%5B'authorization'%5D%5B1%5D%0A  nextnonce = json%5B'head'%5D%5B'authorization'%5D%5B2%5D%0A%0A  username, cnonce, response = test_utils.create_credentials(%0A      test_utils.ADMIN_PASSKEY, test_utils.ADMIN_USERNAME,%0A      nonce, nextnonce)%0A%0A  # Remove the secondary test db.%0A  response = test_utils.make_http_request(%0A      method='POST',%0A      url='/databases/%25s'%25 DATABASE_TOO,%0A      body='%7B%22head%22:%7B%22method%22:%22delete%22, %22authorization%22:%5B%22%25s%22,%22%25s%22,%22%25s%22%5D%7D%7D'%25 %5C%0A          (username, cnonce, response),%0A      headers=%7B%0A        'User-Agent': 'UA:DCube teardown :: remove secondary test db.',%0A        'Accept': 'application/jsonrequest',%0A        'Content-Type': 'application/jsonrequest'%7D)%0A  assert response.status == 200%0A  json = simplejson.loads(response.body)%0A  assert json%5B'head'%5D%5B'status'%5D == 204%0A%0A

Update teardown script to remove secondary db.