Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 85 lines (68 sloc) 2.89 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{ assert, brains, Browser } = require("./helpers")


describe "Authentication", ->

  describe "basic", ->
    before (done)->
      brains.get "/auth/basic", (req, res) ->
        if auth = req.headers.authorization
          if auth == "Basic dXNlcm5hbWU6cGFzczEyMw=="
            res.send "<html><body>#{req.headers["authorization"]}</body></html>"
          else
            res.send "Invalid credentials", 401
        else
          res.send "Missing credentials", 401
      brains.ready done

    describe "without credentials", ->
      browser = new Browser()
      before (done)->
        browser.visit "http://localhost:3003/auth/basic", ->
          done()

      it "should return status code 401", ->
        assert.equal browser.statusCode, 401

    describe "with invalid credentials", ->
      browser = new Browser()
      before (done)->
        credentials = { scheme: "basic", user: "username", password: "wrong" }
        browser.visit "http://localhost:3003/auth/basic", credentials: credentials, ->
          done()

      it "should return status code 401", ->
        assert.equal browser.statusCode, 401

    describe "with valid credentials", ->
      browser = new Browser()
      before (done)->
        credentials = { scheme: "basic", user: "username", password: "pass123" }
        browser.visit "http://localhost:3003/auth/basic", credentials: credentials, done

      it "should have the authentication header", ->
        assert.equal browser.text("body"), "Basic dXNlcm5hbWU6cGFzczEyMw=="


  describe "OAuth bearer", ->
    before (done)->
      brains.get "/auth/oauth2", (req, res) ->
        if auth = req.headers.authorization
          if auth == "Bearer 12345"
            res.send "<html><body>#{req.headers["authorization"]}</body></html>"
          else
            res.send "Invalid token", 401
        else
          res.send "Missing token", 401
      brains.ready done

    describe "without credentials", ->
      browser = new Browser()
      before (done)->
        browser.visit "http://localhost:3003/auth/oauth2", ->
          done()

      it "should return status code 401", ->
        assert.equal browser.statusCode, 401

    describe "with invalid credentials", ->
      browser = new Browser()
      before (done)->
        credentials = { scheme: "bearer", token: "wrong" }
        browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, ->
          done()

      it "should return status code 401", ->
        assert.equal browser.statusCode, 401

    describe "with valid credentials", ->
      browser = new Browser()
      before (done)->
        credentials = { scheme: "bearer", token: "12345" }
        browser.visit "http://localhost:3003/auth/oauth2", credentials: credentials, done

      it "should have the authentication header", ->
        assert.equal browser.text("body"), "Bearer 12345"
Something went wrong with that request. Please try again.