   # Check whether the registration is successful.
   assert_message = 'User is registered can see own profile.'
   casper.waitForUrl /\/user\/[0-9]+\/edit$/,
-    ->
-      # Success.
-      test.assertExists '.status', assert_message
-      saveUserUid()
-    ->
+    -> test.assertExists '.status', assert_message # Success
+    -> # Fail
       @echo 'Registration form errors:', 'ERROR'
       @echo @fetchText('.error').trim(), 'WARNING'
       test.assert false, assert_message

  return

# ------------------------------------------------------------------------
# Test user created from SSO

casper.test.begin "Test that user created from SSO has correct data", 9, (test) ->

  # Launch browser on registration page.
  casper.start url

  # Test the nid is present
  test.assertTruthy uid, "User id from the signup test found."

  # Check user's country.
  login test
  country test
  phantom.clearCookies()

  # Delete user account.
  casper.then ->
    @logAction "Remove the user to test login using remote account only:"
    @deleteUser uid

  # Perform the login.
  login test
  country test

  # Go to the edit profile page.
  # We don't know new uid yet, but user/register will redirect to the page.
  casper.thenOpen "#{url}/user/register"

  # Ensure user data is consistent with the original one.
  casper.then ->
    test.assertField FIELD_FIRST_NAME, user.first_name,
      "Test if user has correct first name."

    test.assertField FIELD_LAST_NAME, user.last_name,
      "Test if user has correct last name."

    test.assertField FIELD_BIRTHDATE, user.dob.format("DD/MM/YYYY"),
      "Test if user has correct birthdate."

    test.assertField FIELD_POSTCODE, user.postcode,
      "Test if user has correct postcode."

    saveUserUid()
    return

  # Run tests.
  casper.run -> @test.done()
  return


# ------------------------------------------------------------------------
# Test login form

casper.test.begin "Test that user created from SSO can login", 1, (test) ->

  # Launch browser on registration page.
  casper.start url

  # Perform the login.
  login test

  # Cleanup after success.
  casper.then ->
    @logAction "Cleanup after full success:"
    @deleteUser uid

  # Run tests.
  casper.run -> @test.done()
  return


# ------------------------------------------------------------------------
# Test registration form

casper.test.begin "Test the registration form", 3, (test) ->

  # Launch browser on registration page.
  casper.start "#{url}/user/register"
