storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/action_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package policy
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestActionIsObjectAction(t *testing.T) {
    26  	testCases := []struct {
    27  		action         Action
    28  		expectedResult bool
    29  	}{
    30  		{AbortMultipartUploadAction, true},
    31  		{DeleteObjectAction, true},
    32  		{GetObjectAction, true},
    33  		{ListMultipartUploadPartsAction, true},
    34  		{PutObjectAction, true},
    35  		{CreateBucketAction, false},
    36  	}
    37  
    38  	for i, testCase := range testCases {
    39  		result := testCase.action.isObjectAction()
    40  
    41  		if testCase.expectedResult != result {
    42  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    43  		}
    44  	}
    45  }
    46  
    47  func TestActionIsValid(t *testing.T) {
    48  	testCases := []struct {
    49  		action         Action
    50  		expectedResult bool
    51  	}{
    52  		{AbortMultipartUploadAction, true},
    53  		{Action("foo"), false},
    54  	}
    55  
    56  	for i, testCase := range testCases {
    57  		result := testCase.action.IsValid()
    58  
    59  		if testCase.expectedResult != result {
    60  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    61  		}
    62  	}
    63  }
    64  
    65  func TestActionMarshalJSON(t *testing.T) {
    66  	testCases := []struct {
    67  		action         Action
    68  		expectedResult []byte
    69  		expectErr      bool
    70  	}{
    71  		{PutObjectAction, []byte(`"s3:PutObject"`), false},
    72  		{Action("foo"), nil, true},
    73  	}
    74  
    75  	for i, testCase := range testCases {
    76  		result, err := json.Marshal(testCase.action)
    77  		expectErr := (err != nil)
    78  
    79  		if testCase.expectErr != expectErr {
    80  			t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    81  		}
    82  
    83  		if !testCase.expectErr {
    84  			if !reflect.DeepEqual(result, testCase.expectedResult) {
    85  				t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    86  			}
    87  		}
    88  	}
    89  }
    90  
    91  func TestActionUnmarshalJSON(t *testing.T) {
    92  	testCases := []struct {
    93  		data           []byte
    94  		expectedResult Action
    95  		expectErr      bool
    96  	}{
    97  		{[]byte(`"s3:PutObject"`), PutObjectAction, false},
    98  		{[]byte(`"foo"`), Action(""), true},
    99  	}
   100  
   101  	for i, testCase := range testCases {
   102  		var result Action
   103  		err := json.Unmarshal(testCase.data, &result)
   104  		expectErr := (err != nil)
   105  
   106  		if testCase.expectErr != expectErr {
   107  			t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   108  		}
   109  
   110  		if !testCase.expectErr {
   111  			if testCase.expectedResult != result {
   112  				t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   113  			}
   114  		}
   115  	}
   116  }