20183: Move priority update thread from rails to controller.
[arvados.git] / lib / controller / federation / login_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package federation
6
7 import (
8         "context"
9         "net/url"
10
11         "git.arvados.org/arvados.git/lib/ctrlctx"
12         "git.arvados.org/arvados.git/sdk/go/arvados"
13         "git.arvados.org/arvados.git/sdk/go/arvadostest"
14         "git.arvados.org/arvados.git/sdk/go/auth"
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&LoginSuite{})
19
20 type LoginSuite struct {
21         FederationSuite
22 }
23
24 func (s *LoginSuite) TestDeferToLoginCluster(c *check.C) {
25         s.addHTTPRemote(c, "zhome", &arvadostest.APIStub{})
26         s.cluster.Login.LoginCluster = "zhome"
27
28         returnTo := "https://app.example.com/foo?bar"
29         for _, remote := range []string{"", "ccccc"} {
30                 resp, err := s.fed.Login(context.Background(), arvados.LoginOptions{Remote: remote, ReturnTo: returnTo})
31                 c.Check(err, check.IsNil)
32                 c.Logf("remote %q -- RedirectLocation %q", remote, resp.RedirectLocation)
33                 target, err := url.Parse(resp.RedirectLocation)
34                 c.Check(err, check.IsNil)
35                 c.Check(target.Host, check.Equals, s.cluster.RemoteClusters["zhome"].Host)
36                 c.Check(target.Scheme, check.Equals, "http")
37                 c.Check(target.Query().Get("return_to"), check.Equals, returnTo)
38                 c.Check(target.Query().Get("remote"), check.Equals, remote)
39                 _, remotePresent := target.Query()["remote"]
40                 c.Check(remotePresent, check.Equals, remote != "")
41         }
42 }
43
44 func (s *LoginSuite) TestLogout(c *check.C) {
45         otherOrigin := arvados.URL{Scheme: "https", Host: "app.example.com", Path: "/"}
46         otherURL := "https://app.example.com/foo"
47         s.cluster.Services.Workbench1.ExternalURL = arvados.URL{Scheme: "https", Host: "workbench1.example.com"}
48         s.cluster.Services.Workbench2.ExternalURL = arvados.URL{Scheme: "https", Host: "workbench2.example.com"}
49         s.cluster.Login.TrustedClients = map[arvados.URL]struct{}{otherOrigin: {}}
50         s.addHTTPRemote(c, "zhome", &arvadostest.APIStub{})
51         s.cluster.Login.LoginCluster = "zhome"
52         // s.fed is already set by SetUpTest, but we need to
53         // reinitialize with the above config changes.
54         s.fed = New(s.ctx, s.cluster, nil, (&ctrlctx.DBConnector{PostgreSQL: s.cluster.PostgreSQL}).GetDB)
55
56         for _, trial := range []struct {
57                 token    string
58                 returnTo string
59                 target   string
60         }{
61                 {token: "", returnTo: "", target: s.cluster.Services.Workbench2.ExternalURL.String()},
62                 {token: "", returnTo: otherURL, target: otherURL},
63                 {token: "zzzzzzzzzzzzzzzzzzzzz", returnTo: otherURL, target: otherURL},
64                 {token: "v2/zzzzz-aaaaa-aaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", returnTo: otherURL, target: otherURL},
65                 {token: "v2/zhome-aaaaa-aaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", returnTo: otherURL, target: "http://" + s.cluster.RemoteClusters["zhome"].Host + "/logout?" + url.Values{"return_to": {otherURL}}.Encode()},
66         } {
67                 c.Logf("trial %#v", trial)
68                 ctx := s.ctx
69                 if trial.token != "" {
70                         ctx = auth.NewContext(ctx, &auth.Credentials{Tokens: []string{trial.token}})
71                 }
72                 resp, err := s.fed.Logout(ctx, arvados.LogoutOptions{ReturnTo: trial.returnTo})
73                 c.Assert(err, check.IsNil)
74                 c.Logf("  RedirectLocation %q", resp.RedirectLocation)
75                 target, err := url.Parse(resp.RedirectLocation)
76                 c.Check(err, check.IsNil)
77                 c.Check(target.String(), check.Equals, trial.target)
78         }
79 }