 
   context 'when I visit the homepage', ->
     before (done) ->
-      browser.visit BASE_URL, done
-
-    context 'when I am not logged in', ->
-      it 'shows me a login form', ->
-        should.exist browser.query('form')
+      browser.get LOGIN_URL, done
 
     context 'when I try to login with valid details', ->
       before (done) ->
-        browser.fill '#username', 'ickletest'
-        browser.fill '#password', 'toottoot'
-        browser.pressButton '#login', ->
-          browser.wait done
-
-      it 'shows my name', ->
-        browser.text('#subnav-path').should.include 'Mr Ickle Test'
+        wd40.fill '#username', 'ickletest', ->
+          wd40.fill '#password', 'toottoot', ->
+            wd40.click '#login', ->
+              browser.wait done
+
+      it 'shows my name', (done) ->
+        wd40.text '#subnav-path', (err, text) ->
+          text.should.include 'Mr Ickle Test'
+          done()
 
       context 'when I logout', ->
         before (done) ->
-          browser.clickLink '#header .logout a', done
-
-        it 'redirects me to the login page', ->
-          browser.location.href.should.equal "#{BASE_URL}/login"
-
-        context 'when I revisit the index page', ->
-          before (done) ->
-            browser.visit BASE_URL, done
-
-          it 'should still present a login form', ->
-            should.exist browser.query('.login-page form')
-            should.not.exist browser.query('.dataset-list')
+          wd40.click '#header .logout a', done
+
+        it 'redirects me to the login page', (done) ->
+          wd40.trueURL (err, url) ->
+            url.should.equal LOGIN_URL
+            done()
 
      xcontext 'when I try to login with my email address as my username', ->
 

          form: form
        , (err, resp, body) =>
          obj = JSON.parse body
          @browser = new Browser()
          @browser.visit "#{BASE_URL}/set-password/#{obj.token}", =>
            @browser.wait done

    it 'shows a page with a password field', ->
      should.exist @browser.query('#password')

    context 'when I fill in my new password', ->
      before (done) ->
        @browser.fill '#password', newPass
        @browser.pressButton '#content .btn-primary', =>
          @browser.wait done

      it 'sets my password', ->
        should.exist @browser.query('.alert-success')


describe 'Switch', ->


      , done

  before (done) ->
    browser.deleteAllCookies done

  before (done) ->
    # Log in as B via zombie
    browser.get LOGIN_URL, ->
      wd40.fill '#username', staff_user, ->
        wd40.fill '#password', staff_pass, ->
          wd40.pressButton '#login', done

  context 'when a staff member switches context', ->
    before (done) ->
      browser.get "#{BASE_URL}/switch/#{nonstaff_user}", done

    it 'redirected to home page', (done) ->
      wd40.trueURL (err, url) ->
        url.should.equal "#{BASE_URL}/"
        done()

    it 'shows me datasets of the profile into which I have switched', (done) ->
      wd40.text '.dataset-list', (err, text) ->
        text.should.include dataset_name
        done()

    it "has the switched to profile's name", (done) ->
      wd40.text 'h1', (err, text) ->
        text.should.include 'Mr Ickle Test'
        done()

    it 'shows a gravatar', (done) ->
      browser.elementByCssIfExists 'h1 img', (err, img) ->
        img.src.should.include 'gravatar'
        done()

    it 'shows the context search box', (done) ->
      browser.waitForVisibleByCss '.context-switch', 4000, done

  context 'when a non-staff member attempts to switch context', ->
    before (done) ->
      browser.deleteAllCookies done

    before (done) ->
      browser.get LOGIN_URL, ->
        wd40.fill '#username', nonstaff_user, ->
          wd40.fill '#password', nonstaff_pass, ->
            wd40.click '#login', ->
              wd40.get "#{BASE_URL}/switch/#{staff_user}", ->
                wd40.get BASE_URL, done

    it "hasn't changed who I am", (done) ->
      wd40.text 'h1', (err, text) ->
        text.should.include 'Mr Ickle Test'
        wd40.text 'h1', (err, text) ->
          text.should.not.include 'Staff Test'
          done()

    it 'still shows me my datasets', (done) ->
      wd40.text '.dataset-list', (err, text) ->
        text.should.include dataset_name
        done()

    it "doesn't show the context switching popup", (done) ->
      browser.isVisible '.context-switch', (err, visible) ->
        visible.should.be.true
        done()

describe 'Whitelabel', ->

