github.com/minio/console@v1.3.0/integration/buckets_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 integration 18 19 import ( 20 "bytes" 21 b64 "encoding/base64" 22 "encoding/json" 23 "fmt" 24 "io" 25 "log" 26 "net/http" 27 "os" 28 "strconv" 29 "testing" 30 "time" 31 32 "github.com/go-openapi/loads" 33 "github.com/minio/console/api" 34 "github.com/minio/console/api/operations" 35 ) 36 37 var token string 38 39 func encodeBase64(fileName string) string { 40 /* 41 Helper function to encode in base64 the file name so we can get the path 42 */ 43 path := b64.StdEncoding.EncodeToString([]byte(fileName)) 44 return path 45 } 46 47 func inspectHTTPResponse(httpResponse *http.Response) string { 48 /* 49 Helper function to inspect the content of a HTTP response. 50 */ 51 b, err := io.ReadAll(httpResponse.Body) 52 if err != nil { 53 log.Fatalln(err) 54 } 55 return "Http Response: " + string(b) 56 } 57 58 func initConsoleServer() (*api.Server, error) { 59 // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") 60 61 swaggerSpec, err := loads.Embedded(api.SwaggerJSON, api.FlatSwaggerJSON) 62 if err != nil { 63 return nil, err 64 } 65 66 noLog := func(string, ...interface{}) { 67 // nothing to log 68 } 69 70 // Initialize MinIO loggers 71 api.LogInfo = noLog 72 api.LogError = noLog 73 74 consoleAPI := operations.NewConsoleAPI(swaggerSpec) 75 consoleAPI.Logger = noLog 76 77 server := api.NewServer(consoleAPI) 78 // register all APIs 79 server.ConfigureAPI() 80 81 // api.GlobalRootCAs, api.GlobalPublicCerts, api.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts 82 83 consolePort, _ := strconv.Atoi("9090") 84 85 server.Host = "0.0.0.0" 86 server.Port = consolePort 87 api.Port = "9090" 88 api.Hostname = "0.0.0.0" 89 90 return server, nil 91 } 92 93 func TestMain(m *testing.M) { 94 // start console server 95 go func() { 96 fmt.Println("start server") 97 srv, err := initConsoleServer() 98 if err != nil { 99 log.Println(err) 100 log.Println("init fail") 101 return 102 } 103 srv.Serve() 104 }() 105 106 fmt.Println("sleeping") 107 time.Sleep(2 * time.Second) 108 109 client := &http.Client{ 110 Timeout: 2 * time.Second, 111 } 112 // get login credentials 113 114 requestData := map[string]string{ 115 "accessKey": "minioadmin", 116 "secretKey": "minioadmin", 117 } 118 119 requestDataJSON, _ := json.Marshal(requestData) 120 121 requestDataBody := bytes.NewReader(requestDataJSON) 122 123 request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody) 124 if err != nil { 125 log.Println(err) 126 return 127 } 128 129 request.Header.Add("Content-Type", "application/json") 130 131 response, err := client.Do(request) 132 if err != nil { 133 log.Println(err) 134 return 135 } 136 137 if response != nil { 138 for _, cookie := range response.Cookies() { 139 if cookie.Name == "token" { 140 token = cookie.Value 141 break 142 } 143 } 144 } 145 146 if token == "" { 147 log.Println("authentication token not found in cookies response") 148 return 149 } 150 151 code := m.Run() 152 153 requestDataAdd := map[string]interface{}{ 154 "name": "test1", 155 } 156 157 requestDataJSON, _ = json.Marshal(requestDataAdd) 158 159 requestDataBody = bytes.NewReader(requestDataJSON) 160 161 // delete bucket 162 request, err = http.NewRequest("DELETE", "http://localhost:9090/api/v1/buckets/test1", requestDataBody) 163 if err != nil { 164 log.Println(err) 165 return 166 } 167 168 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 169 request.Header.Add("Content-Type", "application/json") 170 171 response, err = client.Do(request) 172 if err != nil { 173 log.Println(err) 174 return 175 } 176 177 if response != nil { 178 fmt.Println("DELETE StatusCode:", response.StatusCode) 179 } 180 181 os.Exit(code) 182 }