github.com/minio/console@v1.4.1/api/admin_releases_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2023 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 "os" 25 "testing" 26 27 "github.com/minio/console/api/operations" 28 release "github.com/minio/console/api/operations/release" 29 "github.com/minio/console/models" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/suite" 32 ) 33 34 type ReleasesTestSuite struct { 35 suite.Suite 36 assert *assert.Assertions 37 currentServer string 38 isServerSet bool 39 getServer *httptest.Server 40 withError bool 41 } 42 43 func (suite *ReleasesTestSuite) SetupSuite() { 44 suite.assert = assert.New(suite.T()) 45 suite.getServer = httptest.NewServer(http.HandlerFunc(suite.getHandler)) 46 suite.currentServer, suite.isServerSet = os.LookupEnv(releaseServiceHostEnvVar) 47 os.Setenv(releaseServiceHostEnvVar, suite.getServer.URL) 48 } 49 50 func (suite *ReleasesTestSuite) TearDownSuite() { 51 if suite.isServerSet { 52 os.Setenv(releaseServiceHostEnvVar, suite.currentServer) 53 } else { 54 os.Unsetenv(releaseServiceHostEnvVar) 55 } 56 } 57 58 func (suite *ReleasesTestSuite) getHandler( 59 w http.ResponseWriter, _ *http.Request, 60 ) { 61 if suite.withError { 62 w.WriteHeader(400) 63 } else { 64 w.WriteHeader(200) 65 response := &models.ReleaseListResponse{} 66 bytes, _ := json.Marshal(response) 67 fmt.Fprint(w, string(bytes)) 68 } 69 } 70 71 func (suite *ReleasesTestSuite) TestRegisterReleasesHandlers() { 72 api := &operations.ConsoleAPI{} 73 suite.assert.Nil(api.ReleaseListReleasesHandler) 74 registerReleasesHandlers(api) 75 suite.assert.NotNil(api.ReleaseListReleasesHandler) 76 } 77 78 func (suite *ReleasesTestSuite) TestGetReleasesWithError() { 79 api := &operations.ConsoleAPI{} 80 current := "mock" 81 registerReleasesHandlers(api) 82 params := release.NewListReleasesParams() 83 params.Current = ¤t 84 params.HTTPRequest = &http.Request{} 85 suite.withError = true 86 response := api.ReleaseListReleasesHandler.Handle(params, &models.Principal{}) 87 _, ok := response.(*release.ListReleasesDefault) 88 suite.assert.True(ok) 89 } 90 91 func (suite *ReleasesTestSuite) TestGetReleasesWithoutError() { 92 api := &operations.ConsoleAPI{} 93 registerReleasesHandlers(api) 94 params := release.NewListReleasesParams() 95 params.HTTPRequest = &http.Request{} 96 suite.withError = false 97 response := api.ReleaseListReleasesHandler.Handle(params, &models.Principal{}) 98 _, ok := response.(*release.ListReleasesOK) 99 suite.assert.True(ok) 100 } 101 102 func TestReleases(t *testing.T) { 103 suite.Run(t, new(ReleasesTestSuite)) 104 }