2e08cea484c0fd93403d85dd8295e7426edfeb21
[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         client.Timeout = 0 // disable auto-retry
22         pc := newPermChecker(*client).(*cachingPermChecker)
23         setToken := func(label, token string) {
24                 c.Logf("...%s token %q", label, token)
25                 pc.SetToken(token)
26         }
27         wantError := func(uuid string) {
28                 c.Log(uuid)
29                 ok, err := pc.Check(context.Background(), uuid)
30                 c.Check(ok, check.Equals, false)
31                 c.Check(err, check.NotNil)
32         }
33         wantYes := func(uuid string) {
34                 c.Log(uuid)
35                 ok, err := pc.Check(context.Background(), uuid)
36                 c.Check(ok, check.Equals, true)
37                 c.Check(err, check.IsNil)
38         }
39         wantNo := func(uuid string) {
40                 c.Log(uuid)
41                 ok, err := pc.Check(context.Background(), uuid)
42                 c.Check(ok, check.Equals, false)
43                 c.Check(err, check.IsNil)
44         }
45
46         setToken("no", "")
47         wantNo(arvadostest.UserAgreementCollection)
48         wantNo(arvadostest.UserAgreementPDH)
49         wantNo(arvadostest.FooBarDirCollection)
50
51         setToken("anonymous", arvadostest.AnonymousToken)
52         wantYes(arvadostest.UserAgreementCollection)
53         wantYes(arvadostest.UserAgreementPDH)
54         wantNo(arvadostest.FooBarDirCollection)
55         wantNo(arvadostest.FooCollection)
56
57         setToken("active", arvadostest.ActiveToken)
58         wantYes(arvadostest.UserAgreementCollection)
59         wantYes(arvadostest.UserAgreementPDH)
60         wantYes(arvadostest.FooBarDirCollection)
61         wantYes(arvadostest.FooCollection)
62
63         setToken("admin", arvadostest.AdminToken)
64         wantYes(arvadostest.UserAgreementCollection)
65         wantYes(arvadostest.UserAgreementPDH)
66         wantYes(arvadostest.FooBarDirCollection)
67         wantYes(arvadostest.FooCollection)
68
69         // hack to empty the cache
70         pc.SetToken("")
71         pc.SetToken(arvadostest.ActiveToken)
72
73         c.Log("...network error")
74         pc.Client.APIHost = "127.0.0.1:9"
75         wantError(arvadostest.UserAgreementCollection)
76         wantError(arvadostest.FooBarDirCollection)
77
78         c.Logf("%d checks, %d misses, %d invalid, %d cached", pc.nChecks, pc.nMisses, pc.nInvalid, len(pc.cache))
79 }