...

Source file src/cloud.google.com/go/storage/acl_test.go

Documentation: cloud.google.com/go/storage

     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  
    15  package storage
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"cloud.google.com/go/internal/testutil"
    23  	"cloud.google.com/go/storage/internal/apiv2/storagepb"
    24  )
    25  
    26  func TestSetACL(t *testing.T) {
    27  	ctx := context.Background()
    28  	mt := &mockTransport{}
    29  	client := mockClient(t, mt)
    30  	bh := &ACLHandle{c: client, bucket: "B"}
    31  	oh := &ACLHandle{c: client, bucket: "B", object: "O"}
    32  	for _, test := range []struct {
    33  		desc string
    34  		f    func() error
    35  		want map[string]interface{}
    36  	}{
    37  		{
    38  			desc: "bucket Set",
    39  			f:    func() error { return bh.Set(ctx, AllUsers, RoleReader) },
    40  			want: map[string]interface{}{
    41  				"bucket": "B",
    42  				"entity": "allUsers",
    43  				"role":   "READER",
    44  			},
    45  		},
    46  
    47  		{
    48  			desc: "object Set",
    49  			f:    func() error { return oh.Set(ctx, ACLEntity("e"), RoleWriter) },
    50  			want: map[string]interface{}{
    51  				"bucket": "B",
    52  				"entity": "e",
    53  				"role":   "WRITER",
    54  			},
    55  		}} {
    56  
    57  		mt.addResult(&http.Response{StatusCode: 200, Body: bodyReader("{}")}, nil)
    58  		if err := test.f(); err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		got := mt.gotJSONBody()
    62  		if diff := testutil.Diff(got, test.want); diff != "" {
    63  			t.Errorf("%s: %s", test.desc, diff)
    64  		}
    65  	}
    66  }
    67  
    68  func TestToProtoObjectACL(t *testing.T) {
    69  	for i, tst := range []struct {
    70  		rules []ACLRule
    71  		want  []*storagepb.ObjectAccessControl
    72  	}{
    73  		{nil, nil},
    74  		{
    75  			rules: []ACLRule{
    76  				{Entity: "foo", Role: "bar", Domain: "do not copy me!", Email: "donotcopy@"},
    77  				{Entity: "bar", Role: "foo", ProjectTeam: &ProjectTeam{ProjectNumber: "1234", Team: "donotcopy"}},
    78  			},
    79  			want: []*storagepb.ObjectAccessControl{
    80  				{Entity: "foo", Role: "bar"},
    81  				{Entity: "bar", Role: "foo"},
    82  			},
    83  		},
    84  	} {
    85  		got := toProtoObjectACL(tst.rules)
    86  		if diff := testutil.Diff(got, tst.want); diff != "" {
    87  			t.Errorf("#%d: got(-),want(+):\n%s", i, diff)
    88  		}
    89  	}
    90  }
    91  
    92  func TestFromProtoToObjectACLRules(t *testing.T) {
    93  	for i, tst := range []struct {
    94  		want []ACLRule
    95  		acls []*storagepb.ObjectAccessControl
    96  	}{
    97  		{nil, nil},
    98  		{
    99  			want: []ACLRule{
   100  				{Entity: "foo", Role: "bar", ProjectTeam: &ProjectTeam{ProjectNumber: "1234", Team: "foo"}},
   101  				{Entity: "bar", Role: "foo", EntityID: "baz", Domain: "domain"},
   102  			},
   103  			acls: []*storagepb.ObjectAccessControl{
   104  				{Entity: "foo", Role: "bar", ProjectTeam: &storagepb.ProjectTeam{ProjectNumber: "1234", Team: "foo"}},
   105  				{Entity: "bar", Role: "foo", EntityId: "baz", Domain: "domain"},
   106  			},
   107  		},
   108  	} {
   109  		got := toObjectACLRulesFromProto(tst.acls)
   110  		if diff := testutil.Diff(got, tst.want); diff != "" {
   111  			t.Errorf("#%d: got(-),want(+):\n%s", i, diff)
   112  		}
   113  	}
   114  }
   115  

View as plain text