1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
8 "git.arvados.org/arvados.git/lib/ctrlctx"
9 "git.arvados.org/arvados.git/sdk/go/arvados"
10 "git.arvados.org/arvados.git/sdk/go/arvadostest"
11 check "gopkg.in/check.v1"
14 var _ = check.Suite(&GroupSuite{})
16 type GroupSuite struct {
20 func (s *GroupSuite) TestGroupCreateWithProperties(c *check.C) {
21 s.setUpVocabulary(c, "")
25 props map[string]interface{}
28 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
29 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
30 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
31 {"Empty properties", map[string]interface{}{}, true},
33 for _, tt := range tests {
34 c.Log(c.TestName()+" ", tt.name)
36 grp, err := s.localdb.GroupCreate(s.userctx, arvados.CreateOptions{
37 Select: []string{"uuid", "properties"},
38 Attrs: map[string]interface{}{
39 "group_class": "project",
40 "properties": tt.props,
43 c.Assert(err, check.IsNil)
44 c.Assert(grp.Properties, check.DeepEquals, tt.props)
46 c.Assert(err, check.NotNil)
51 func (s *GroupSuite) TestGroupUpdateWithProperties(c *check.C) {
52 s.setUpVocabulary(c, "")
56 props map[string]interface{}
59 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
60 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
61 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
62 {"Empty properties", map[string]interface{}{}, true},
64 for _, tt := range tests {
65 c.Log(c.TestName()+" ", tt.name)
66 grp, err := s.localdb.GroupCreate(s.userctx, arvados.CreateOptions{
67 Attrs: map[string]interface{}{
68 "group_class": "project",
71 c.Assert(err, check.IsNil)
72 grp, err = s.localdb.GroupUpdate(s.userctx, arvados.UpdateOptions{
74 Select: []string{"uuid", "properties"},
75 Attrs: map[string]interface{}{
76 "properties": tt.props,
79 c.Assert(err, check.IsNil)
80 c.Assert(grp.Properties, check.DeepEquals, tt.props)
82 c.Assert(err, check.NotNil)
87 func (s *GroupSuite) TestCanWriteCanManageResponses(c *check.C) {
88 ctxUser1 := ctrlctx.NewWithToken(s.ctx, s.cluster, arvadostest.ActiveTokenV2)
89 ctxUser2 := ctrlctx.NewWithToken(s.ctx, s.cluster, arvadostest.SpectatorToken)
90 ctxAdmin := ctrlctx.NewWithToken(s.ctx, s.cluster, arvadostest.AdminToken)
91 project, err := s.localdb.GroupCreate(ctxUser1, arvados.CreateOptions{
92 Attrs: map[string]interface{}{
93 "group_class": "project",
96 c.Assert(err, check.IsNil)
97 c.Check(project.CanWrite, check.Equals, true)
98 c.Check(project.CanManage, check.Equals, true)
100 subproject, err := s.localdb.GroupCreate(ctxUser1, arvados.CreateOptions{
101 Attrs: map[string]interface{}{
102 "owner_uuid": project.UUID,
103 "group_class": "project",
106 c.Assert(err, check.IsNil)
107 c.Check(subproject.CanWrite, check.Equals, true)
108 c.Check(subproject.CanManage, check.Equals, true)
110 projlist, err := s.localdb.GroupList(ctxUser1, arvados.ListOptions{
112 Filters: []arvados.Filter{{"uuid", "in", []string{project.UUID, subproject.UUID}}},
114 c.Assert(err, check.IsNil)
115 c.Assert(projlist.Items, check.HasLen, 2)
116 for _, p := range projlist.Items {
117 c.Check(p.CanWrite, check.Equals, true)
118 c.Check(p.CanManage, check.Equals, true)
121 // Give 2nd user permission to read
122 permlink, err := s.localdb.LinkCreate(ctxAdmin, arvados.CreateOptions{
123 Attrs: map[string]interface{}{
124 "link_class": "permission",
126 "tail_uuid": arvadostest.SpectatorUserUUID,
127 "head_uuid": project.UUID,
130 c.Assert(err, check.IsNil)
132 // As 2nd user: can read, cannot manage, cannot write
133 project2, err := s.localdb.GroupGet(ctxUser2, arvados.GetOptions{UUID: project.UUID})
134 c.Assert(err, check.IsNil)
135 c.Check(project2.CanWrite, check.Equals, false)
136 c.Check(project2.CanManage, check.Equals, false)
138 _, err = s.localdb.LinkUpdate(ctxAdmin, arvados.UpdateOptions{
140 Attrs: map[string]interface{}{
144 c.Assert(err, check.IsNil)
146 // As 2nd user: cannot manage, can write
147 project2, err = s.localdb.GroupGet(ctxUser2, arvados.GetOptions{UUID: project.UUID})
148 c.Assert(err, check.IsNil)
149 c.Check(project2.CanWrite, check.Equals, true)
150 c.Check(project2.CanManage, check.Equals, false)
152 // As owner: after freezing, can manage (owner), cannot write (frozen)
153 project, err = s.localdb.GroupUpdate(ctxUser1, arvados.UpdateOptions{
155 Attrs: map[string]interface{}{
156 "frozen_by_uuid": arvadostest.ActiveUserUUID,
158 c.Assert(err, check.IsNil)
159 c.Check(project.CanWrite, check.Equals, false)
160 c.Check(project.CanManage, check.Equals, true)
162 // As admin: can manage (admin), cannot write (frozen)
163 project, err = s.localdb.GroupGet(ctxAdmin, arvados.GetOptions{UUID: project.UUID})
164 c.Assert(err, check.IsNil)
165 c.Check(project.CanWrite, check.Equals, false)
166 c.Check(project.CanManage, check.Equals, true)
168 // As 2nd user: cannot manage (perm), cannot write (frozen)
169 project2, err = s.localdb.GroupGet(ctxUser2, arvados.GetOptions{UUID: project.UUID})
170 c.Assert(err, check.IsNil)
171 c.Check(project2.CanWrite, check.Equals, false)
172 c.Check(project2.CanManage, check.Equals, false)
174 // After upgrading perm to "manage", as 2nd user: can manage (perm), cannot write (frozen)
175 _, err = s.localdb.LinkUpdate(ctxAdmin, arvados.UpdateOptions{
177 Attrs: map[string]interface{}{
178 "name": "can_manage",
181 c.Assert(err, check.IsNil)
182 project2, err = s.localdb.GroupGet(ctxUser2, arvados.GetOptions{UUID: project.UUID})
183 c.Assert(err, check.IsNil)
184 c.Check(project2.CanWrite, check.Equals, false)
185 c.Check(project2.CanManage, check.Equals, true)
187 // 2nd user can also manage (but not write) the subject inside the frozen project
188 subproject2, err := s.localdb.GroupGet(ctxUser2, arvados.GetOptions{UUID: subproject.UUID})
189 c.Assert(err, check.IsNil)
190 c.Check(subproject2.CanWrite, check.Equals, false)
191 c.Check(subproject2.CanManage, check.Equals, true)
193 u, err := s.localdb.UserGet(ctxUser1, arvados.GetOptions{
194 UUID: arvadostest.ActiveUserUUID,
196 c.Assert(err, check.IsNil)
197 c.Check(u.CanWrite, check.Equals, true)
198 c.Check(u.CanManage, check.Equals, true)
200 for _, selectParam := range [][]string{
202 {"can_write", "can_manage"},
204 c.Logf("selectParam: %+v", selectParam)
205 ulist, err := s.localdb.UserList(ctxUser1, arvados.ListOptions{
207 Filters: []arvados.Filter{{"uuid", "=", arvadostest.ActiveUserUUID}},
210 c.Assert(err, check.IsNil)
211 c.Assert(ulist.Items, check.HasLen, 1)
212 c.Logf("%+v", ulist.Items)
213 for _, u := range ulist.Items {
214 c.Check(u.CanWrite, check.Equals, true)
215 c.Check(u.CanManage, check.Equals, true)