Merge branch '20846-ubuntu2204'
[arvados.git] / services / ws / permission_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package ws
6
7 import (
8         "context"
9
10         "git.arvados.org/arvados.git/sdk/go/arvados"
11         "git.arvados.org/arvados.git/sdk/go/arvadostest"
12         check "gopkg.in/check.v1"
13 )
14
15 var _ = check.Suite(&permSuite{})
16
17 type permSuite struct{}
18
19 func (s *permSuite) TestCheck(c *check.C) {
20         client := arvados.NewClientFromEnv()
21         // Disable auto-retry
22         client.Timeout = 0
23
24         pc := newPermChecker(client).(*cachingPermChecker)
25         setToken := func(label, token string) {
26                 c.Logf("...%s token %q", label, token)
27                 pc.SetToken(token)
28         }
29         wantError := func(uuid string) {
30                 c.Log(uuid)
31                 ok, err := pc.Check(context.Background(), uuid)
32                 c.Check(ok, check.Equals, false)
33                 c.Check(err, check.NotNil)
34         }
35         wantYes := func(uuid string) {
36                 c.Log(uuid)
37                 ok, err := pc.Check(context.Background(), uuid)
38                 c.Check(ok, check.Equals, true)
39                 c.Check(err, check.IsNil)
40         }
41         wantNo := func(uuid string) {
42                 c.Log(uuid)
43                 ok, err := pc.Check(context.Background(), uuid)
44                 c.Check(ok, check.Equals, false)
45                 c.Check(err, check.IsNil)
46         }
47
48         setToken("no", "")
49         wantNo(arvadostest.UserAgreementCollection)
50         wantNo(arvadostest.UserAgreementPDH)
51         wantNo(arvadostest.FooBarDirCollection)
52
53         setToken("anonymous", arvadostest.AnonymousToken)
54         wantYes(arvadostest.UserAgreementCollection)
55         wantYes(arvadostest.UserAgreementPDH)
56         wantNo(arvadostest.FooBarDirCollection)
57         wantNo(arvadostest.FooCollection)
58
59         setToken("active", arvadostest.ActiveToken)
60         wantYes(arvadostest.UserAgreementCollection)
61         wantYes(arvadostest.UserAgreementPDH)
62         wantYes(arvadostest.FooBarDirCollection)
63         wantYes(arvadostest.FooCollection)
64
65         setToken("admin", arvadostest.AdminToken)
66         wantYes(arvadostest.UserAgreementCollection)
67         wantYes(arvadostest.UserAgreementPDH)
68         wantYes(arvadostest.FooBarDirCollection)
69         wantYes(arvadostest.FooCollection)
70
71         // hack to empty the cache
72         pc.SetToken("")
73         pc.SetToken(arvadostest.ActiveToken)
74
75         c.Log("...network error")
76         pc.ac.APIHost = "127.0.0.1:9"
77         wantError(arvadostest.UserAgreementCollection)
78         wantError(arvadostest.FooBarDirCollection)
79
80         c.Logf("%d checks, %d misses, %d invalid, %d cached", pc.nChecks, pc.nMisses, pc.nInvalid, len(pc.cache))
81 }