github.com/minio/console@v1.4.1/pkg/logger/message/audit/entry_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package audit
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestNewEntry(t *testing.T) {
    25  	type args struct {
    26  		deploymentID string
    27  	}
    28  	tests := []struct {
    29  		name string
    30  		args args
    31  		want Entry
    32  	}{
    33  		{
    34  			name: "constructs an audit entry object with some fields filled",
    35  			args: args{
    36  				deploymentID: "1",
    37  			},
    38  			want: Entry{
    39  				Version:      Version,
    40  				DeploymentID: "1",
    41  				Time:         time.Now().UTC(),
    42  			},
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(_ *testing.T) {
    47  			if got := NewEntry(tt.args.deploymentID); got.DeploymentID != tt.want.DeploymentID {
    48  				t.Errorf("NewEntry() = %v, want %v", got, tt.want)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  // TODO: Please assist in fixing this test whenever you have the opportunity.
    55  // This test hasn't been executed for a long time. Upon its reintroduction in https://github.com/minio/console/pull/3171,
    56  // the test began to fail. I'm uncertain whether this test was passing from the outset or not,
    57  // but it should pass if utilized within our coverage.
    58  // func TestToEntry(t *testing.T) {
    59  // 	req := httptest.NewRequest(http.MethodGet, "/api/v1/tenants?test=xyz", nil)
    60  // 	req.Header.Set("Authorization", "xyz")
    61  // 	req.Header.Set("ETag", "\"ABCDE\"")
    62  
    63  // 	// applying context information
    64  // 	ctx := context.WithValue(req.Context(), utils.ContextRequestUserID, "eyJhbGciOiJSUzI1NiIsImtpZCI6Ing5cS0wSkEwQzFMWDJlRlR3dHo2b0t0NVNnRzJad0llMGVNczMxbjU0b2sifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJtaW5pby1vcGVyYXRvciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJjb25zb2xlLXNhLXRva2VuLWJrZzZwIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImNvbnNvbGUtc2EiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJhZTE2ZGVkNS01MmM3LTRkZTQtOWUxYS1iNmI4NGU2OGMzM2UiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6bWluaW8tb3BlcmF0b3I6Y29uc29sZS1zYSJ9.AjhzekAPC59SQVBQL5sr-1dqr57-jH8a5LVazpnEr_cC0JqT4jXYjdfbrZSF9yaL4gHRv2l0kOhBlrjRK7y-IpMbxE71Fne_lSzaptSuqgI5I9dFvpVfZWP1yMAqav8mrlUoWkWDq9IAkyH4bvvZrVgQJGgd5t9U_7DQCVwbkQvy0wGS5zoMcZhYenn_Ub1BoxWcviADQ1aY1wQju8OP0IOwKTIMXMQqciOFdJ9T5-tQEGUrikTu_tW-1shUHzOxBcEzGVtBvBy2OmbNnRFYogbhmp-Dze6EAi035bY32bfL7XKBUNCW6_3VbN_h3pQNAuT2NJOSKuhJ3cGldCB2zg")
    65  // 	req = req.WithContext(ctx)
    66  
    67  // 	w := httptest.NewRecorder()
    68  // 	w.Header().Set("Authorization", "xyz")
    69  // 	w.Header().Set("ETag", "\"ABCDE\"")
    70  
    71  // 	type args struct {
    72  // 		w            http.ResponseWriter
    73  // 		r            *http.Request
    74  // 		reqClaims    map[string]interface{}
    75  // 		deploymentID string
    76  // 	}
    77  // 	tests := []struct {
    78  // 		name     string
    79  // 		args     args
    80  // 		want     Entry
    81  // 		preFunc  func()
    82  // 		postFunc func()
    83  // 	}{
    84  // 		{
    85  // 			preFunc: func() {
    86  // 				os.Setenv("CONSOLE_OPERATOR_MODE", "on")
    87  // 			},
    88  // 			postFunc: func() {
    89  // 				os.Unsetenv("CONSOLE_OPERATOR_MODE")
    90  // 			},
    91  // 			name: "constructs an audit entry from a http request",
    92  // 			args: args{
    93  // 				w:            w,
    94  // 				r:            req,
    95  // 				reqClaims:    map[string]interface{}{},
    96  // 				deploymentID: "1",
    97  // 			},
    98  // 			want: Entry{
    99  // 				Version:      "1",
   100  // 				DeploymentID: "1",
   101  // 				SessionID:    "system:serviceaccount:minio-operator:console-sa",
   102  // 				ReqQuery:     map[string]string{"test": "xyz"},
   103  // 				ReqHeader:    map[string]string{"test": "xyz"},
   104  // 				RespHeader:   map[string]string{"test": "xyz", "ETag": "ABCDE"},
   105  // 			},
   106  // 		},
   107  // 	}
   108  // 	for _, tt := range tests {
   109  // 		t.Run(tt.name, func(_ *testing.T) {
   110  // 			if tt.preFunc != nil {
   111  // 				tt.preFunc()
   112  // 			}
   113  // 			if got := ToEntry(tt.args.w, tt.args.r, tt.args.reqClaims, tt.args.deploymentID); !reflect.DeepEqual(got, tt.want) {
   114  // 				t.Errorf("ToEntry() = %v, want %v", got, tt.want)
   115  // 			}
   116  // 			if tt.postFunc != nil {
   117  // 				tt.postFunc()
   118  // 			}
   119  // 		})
   120  // 	}
   121  // }