1// Copyright 2014 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package storage
16
17import (
18	"context"
19	"net/http"
20	"testing"
21
22	"cloud.google.com/go/internal/testutil"
23)
24
25func TestSetACL(t *testing.T) {
26	ctx := context.Background()
27	mt := &mockTransport{}
28	client := mockClient(t, mt)
29	bh := &ACLHandle{c: client, bucket: "B"}
30	oh := &ACLHandle{c: client, bucket: "B", object: "O"}
31	for _, test := range []struct {
32		desc string
33		f    func() error
34		want map[string]interface{}
35	}{
36		{
37			desc: "bucket Set",
38			f:    func() error { return bh.Set(ctx, AllUsers, RoleReader) },
39			want: map[string]interface{}{
40				"bucket": "B",
41				"entity": "allUsers",
42				"role":   "READER",
43			},
44		},
45
46		{
47			desc: "object Set",
48			f:    func() error { return oh.Set(ctx, ACLEntity("e"), RoleWriter) },
49			want: map[string]interface{}{
50				"bucket": "B",
51				"entity": "e",
52				"role":   "WRITER",
53			},
54		}} {
55
56		mt.addResult(&http.Response{StatusCode: 200, Body: bodyReader("{}")}, nil)
57		if err := test.f(); err != nil {
58			t.Fatal(err)
59		}
60		got := mt.gotJSONBody()
61		if diff := testutil.Diff(got, test.want); diff != "" {
62			t.Errorf("%s: %s", test.desc, diff)
63		}
64	}
65}
66