github.com/minio/console@v1.3.0/api/user_log_search_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 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 api
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/minio/console/models"
    28  )
    29  
    30  func TestLogSearch(t *testing.T) {
    31  	responseItem := []map[string]interface{}{
    32  		{
    33  			"time":                    "2006-01-02T15:04:05Z",
    34  			"api_time":                "GetConfigKV",
    35  			"bucket":                  "",
    36  			"object":                  "",
    37  			"time_to_response_ns":     float64(452546530),
    38  			"remote_host":             "10.116.1.94",
    39  			"request_id":              "16595A4E30CCFE79",
    40  			"user_agent":              "MinIO (linux; amd64) madmin-go/0.0.1",
    41  			"response_status":         "OK",
    42  			"response_status_code":    float64(200),
    43  			"request_content_length":  nil,
    44  			"response_content_length": nil,
    45  		}, {
    46  			"time":                    "2006-01-02T15:04:05Z",
    47  			"api_time":                "AssumeRole",
    48  			"bucket":                  "",
    49  			"object":                  "",
    50  			"time_to_response_ns":     float64(307423794),
    51  			"remote_host":             "127.0.0.1",
    52  			"request_id":              "16595A4DA906FBA9",
    53  			"user_agent":              "Go-http-client/1.1",
    54  			"response_status":         "OK",
    55  			"response_status_code":    float64(200),
    56  			"request_content_length":  nil,
    57  			"response_content_length": nil,
    58  		},
    59  	}
    60  
    61  	type args struct {
    62  		apiResponse     string
    63  		apiResponseCode int
    64  	}
    65  
    66  	response, _ := json.Marshal(responseItem)
    67  
    68  	successfulResponse := &models.LogSearchResponse{
    69  		Results: responseItem,
    70  	}
    71  
    72  	tests := []struct {
    73  		name             string
    74  		args             args
    75  		expectedResponse *models.LogSearchResponse
    76  		wantErr          bool
    77  	}{
    78  		{
    79  			name: "200 Success response",
    80  			args: args{
    81  				apiResponse:     string(response),
    82  				apiResponseCode: 200,
    83  			},
    84  			expectedResponse: successfulResponse,
    85  			wantErr:          false,
    86  		},
    87  		{
    88  			name: "500 unsuccessful response",
    89  			args: args{
    90  				apiResponse:     "Some random error",
    91  				apiResponseCode: 500,
    92  			},
    93  			expectedResponse: nil,
    94  			wantErr:          true,
    95  		},
    96  	}
    97  
    98  	for _, tt := range tests {
    99  		tt := tt
   100  		t.Run(tt.name, func(_ *testing.T) {
   101  			testRequest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
   102  				w.WriteHeader(tt.args.apiResponseCode)
   103  				fmt.Fprintln(w, tt.args.apiResponse)
   104  			}))
   105  			defer testRequest.Close()
   106  
   107  			resp, err := logSearch(testRequest.URL, "127.0.0.1")
   108  
   109  			if (err != nil) != tt.wantErr {
   110  				t.Errorf("logSearch() error = %v, wantErr %v", err, tt.wantErr)
   111  			}
   112  			if !reflect.DeepEqual(resp, tt.expectedResponse) {
   113  				t.Errorf("\ngot: %d \nwant: %d", resp, tt.expectedResponse)
   114  			}
   115  			// if tt.wantErr {
   116  			//	assert.Equal(tt.expectedError.Code, err.Code, fmt.Sprintf("logSearch() error code: `%v`, wantErr: `%v`", err.Code, tt.expectedError))
   117  			//	assert.Equal(tt.expectedError.Message, err.Message, fmt.Sprintf("logSearch() error message: `%v`, wantErr: `%v`", err.Message, tt.expectedError))
   118  			// } else {
   119  			//	assert.Nil(err, fmt.Sprintf("logSearch() error: %v, wantErr: %v", err, tt.expectedError))
   120  			//	buf1, err1 := tt.expectedResponse.MarshalBinary()
   121  			//	buf2, err2 := resp.MarshalBinary()
   122  			//	if err1 != err2 {
   123  			//		t.Errorf("logSearch() resp: %v, expectedResponse: %v", resp, tt.expectedResponse)
   124  			//		return
   125  			//	}
   126  			//	h := sha256.New()
   127  			//	h.Write(buf1)
   128  			//	checkSum1 := fmt.Sprintf("%x\n", h.Sum(nil))
   129  			//	h.Reset()
   130  			//	h.Write(buf2)
   131  			//	checkSum2 := fmt.Sprintf("%x\n", h.Sum(nil))
   132  			//	if checkSum1 != checkSum2 {
   133  			//		t.Errorf("logSearch() resp: %v, expectedResponse: %v", resp, tt.expectedResponse)
   134  			//	}
   135  			// }
   136  		})
   137  	}
   138  }