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 86 87 88 89 90
|
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE.client file for details.
package redirect_test
import (
"context"
"net/http"
"testing"
qt "github.com/frankban/quicktest"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
errgo "gopkg.in/errgo.v1"
"github.com/canonical/candid/candidclient/redirect"
"github.com/canonical/candid/params"
)
func TestRedirectURL(t *testing.T) {
c := qt.New(t)
info := redirect.InteractionInfo{
LoginURL: "https://www.example.com/login",
}
rurl := info.RedirectURL("https://www.example.com/callback", "12345")
c.Assert(rurl, qt.Equals, "https://www.example.com/login?return_to=https%3A%2F%2Fwww.example.com%2Fcallback&state=12345")
info = redirect.InteractionInfo{
LoginURL: "https://www.example.com/login?domain=test",
}
rurl = info.RedirectURL("https://www.example.com/callback", "12345")
c.Assert(rurl, qt.Equals, "https://www.example.com/login?domain=test&return_to=https%3A%2F%2Fwww.example.com%2Fcallback&state=12345")
}
func TestInteractor(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
var i redirect.Interactor
c.Assert(i.Kind(), qt.Equals, redirect.Kind)
req, err := http.NewRequest("GET", "https://www.example.com/discharge", nil)
c.Assert(err, qt.IsNil)
irerr := httpbakery.NewInteractionRequiredError(nil, req)
// Fake an empty InteractionRequiredError
irerr.Info = &httpbakery.ErrorInfo{}
_, err = i.Interact(ctx, nil, "", irerr)
c.Assert(errgo.Cause(err), qt.Equals, httpbakery.ErrInteractionMethodNotFound)
redirect.SetInteraction(irerr, "https://www.example.com/login", "https://www.example.com/token")
_, err = i.Interact(ctx, nil, "", irerr)
c.Assert(err, qt.Satisfies, httpbakery.IsInteractionError)
ierr := err.(*httpbakery.InteractionError)
c.Assert(ierr.Reason, qt.Satisfies, redirect.IsRedirectRequiredError)
c.Assert(ierr.Reason.(*redirect.RedirectRequiredError).InteractionInfo, qt.Equals, redirect.InteractionInfo{
LoginURL: "https://www.example.com/login",
DischargeTokenURL: "https://www.example.com/token",
})
dt := &httpbakery.DischargeToken{
Kind: "test",
Value: []byte("test"),
}
i.SetDischargeToken("https://www.example.com/login", dt)
dt2, err := i.Interact(ctx, nil, "", irerr)
c.Assert(err, qt.IsNil)
c.Assert(*dt2, qt.DeepEquals, *dt)
}
func TestParseLoginResult(t *testing.T) {
c := qt.New(t)
state, code, err := redirect.ParseLoginResult("https://example.com/callback?state=12345&code=54321")
c.Assert(state, qt.Equals, "12345")
c.Assert(err, qt.IsNil)
c.Assert(code, qt.Equals, "54321")
state, code, err = redirect.ParseLoginResult("https://example.com/callback?state=12345&error_code=ec&error=test+error")
c.Assert(state, qt.Equals, "12345")
c.Assert(errgo.Cause(err), qt.Equals, params.ErrorCode("ec"))
c.Assert(err, qt.ErrorMatches, "test error")
c.Assert(code, qt.Equals, "")
state, code, err = redirect.ParseLoginResult("https://example.com/callback?state=12345&error=test+error")
c.Assert(state, qt.Equals, "12345")
c.Assert(err, qt.ErrorMatches, "test error")
c.Assert(code, qt.Equals, "")
}
|