github.com/minio/madmin-go/v2@v2.2.1/policy-commands_test.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  	"testing"
    26  	"time"
    27  )
    28  
    29  var (
    30  	withCreateDate    = []byte(`{"PolicyName":"readwrite","Policy":{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["admin:*"]},{"Effect":"Allow","Action":["s3:*"],"Resource":["arn:aws:s3:::*"]}]},"CreateDate":"2020-03-15T10:10:10Z","UpdateDate":"2021-03-15T10:10:10Z"}`)
    31  	withoutCreateDate = []byte(`{"PolicyName":"readwrite","Policy":{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["admin:*"]},{"Effect":"Allow","Action":["s3:*"],"Resource":["arn:aws:s3:::*"]}]}}`)
    32  )
    33  
    34  func TestPolicyInfo(t *testing.T) {
    35  	testCases := []struct {
    36  		pi          *PolicyInfo
    37  		expectedBuf []byte
    38  	}{
    39  		{
    40  			&PolicyInfo{
    41  				PolicyName: "readwrite",
    42  				Policy:     []byte(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["admin:*"]},{"Effect":"Allow","Action":["s3:*"],"Resource":["arn:aws:s3:::*"]}]}`),
    43  				CreateDate: time.Date(2020, time.March, 15, 10, 10, 10, 0, time.UTC),
    44  				UpdateDate: time.Date(2021, time.March, 15, 10, 10, 10, 0, time.UTC),
    45  			},
    46  			withCreateDate,
    47  		},
    48  		{
    49  			&PolicyInfo{
    50  				PolicyName: "readwrite",
    51  				Policy:     []byte(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["admin:*"]},{"Effect":"Allow","Action":["s3:*"],"Resource":["arn:aws:s3:::*"]}]}`),
    52  			},
    53  			withoutCreateDate,
    54  		},
    55  	}
    56  
    57  	for _, testCase := range testCases {
    58  		testCase := testCase
    59  		t.Run("", func(t *testing.T) {
    60  			buf, err := json.Marshal(testCase.pi)
    61  			if err != nil {
    62  				t.Error(err)
    63  			}
    64  			if !bytes.Equal(buf, testCase.expectedBuf) {
    65  				t.Errorf("expected %s, got %s", string(testCase.expectedBuf), string(buf))
    66  			}
    67  		})
    68  	}
    69  }