github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/organization/cloud_organization_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 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 published by 8 the Free Software Foundation, either version 3 of the License, or 9 (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 organization 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "encoding/json" 27 "net/http" 28 "net/http/httptest" 29 "os" 30 ) 31 32 func mockServer() *httptest.Server { 33 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 34 switch r.URL.Path { 35 case "/api/v1/organizations/test_org": 36 switch r.Method { 37 case http.MethodGet: 38 org := OrgItem{ 39 ID: "test_id", 40 Name: "test_org", 41 Role: "test_role", 42 Description: "test_description", 43 DisplayName: "test_display_name", 44 CreatedAt: "test_created_at", 45 UpdatedAt: "test_updated_at", 46 } 47 48 jsonData, err := json.Marshal(org) 49 if err != nil { 50 w.WriteHeader(http.StatusInternalServerError) 51 return 52 } 53 w.Header().Set("Content-Type", "application/json") 54 w.WriteHeader(http.StatusOK) 55 _, _ = w.Write(jsonData) 56 case http.MethodDelete: 57 w.WriteHeader(http.StatusOK) 58 case http.MethodPost: 59 w.WriteHeader(http.StatusCreated) 60 default: 61 w.WriteHeader(http.StatusNotFound) 62 } 63 64 case "/api/v1/organizations": 65 orgs := &Organizations{ 66 Items: []OrgItem{ 67 { 68 ID: "test_id", 69 Name: "test_org", 70 Role: "test_role", 71 Description: "test_description", 72 DisplayName: "test_display_name", 73 }, 74 }, 75 } 76 jsonData, err := json.Marshal(orgs) 77 if err != nil { 78 w.WriteHeader(http.StatusInternalServerError) 79 return 80 } 81 82 w.Header().Set("Content-Type", "application/json") 83 w.WriteHeader(http.StatusOK) 84 _, _ = w.Write(jsonData) 85 } 86 })) 87 } 88 89 var _ = Describe("Test Cloud Organization", func() { 90 var ( 91 o *CloudOrganization 92 ) 93 BeforeEach(func() { 94 server := mockServer() 95 o = &CloudOrganization{Token: "test_token", APIURL: server.URL, APIPath: APIPath} 96 os.Setenv("TEST_ENV", "true") 97 }) 98 99 AfterEach(func() { 100 defer os.Unsetenv("TEST_ENV") 101 }) 102 103 Context("test cloud organization", func() { 104 Expect(SetCurrentOrgAndContext(&CurrentOrgAndContext{ 105 CurrentOrganization: "test_org", 106 CurrentContext: "test_context", 107 })).Should(BeNil()) 108 109 It("test getOrganization ", func() { 110 ExpectWithOffset(1, func() error { 111 _, err := o.getOrganization("test_org") 112 return err 113 }()).To(BeNil()) 114 }) 115 116 It("test GetOrganizations ", func() { 117 ExpectWithOffset(1, func() error { 118 _, err := o.GetOrganizations() 119 return err 120 }()).To(BeNil()) 121 }) 122 123 It("test GetCurrentOrgAndContext ", func() { 124 ExpectWithOffset(1, func() error { 125 _, err := GetCurrentOrgAndContext() 126 return err 127 }()).To(BeNil()) 128 }) 129 130 It("test switchOrganization ", func() { 131 ExpectWithOffset(1, func() error { 132 _, err := o.switchOrganization("test_org") 133 return err 134 }()).To(BeNil()) 135 }) 136 137 It("test addOrganization ", func() { 138 ExpectWithOffset(1, func() error { 139 org := &OrgItem{ 140 ID: "test_id", 141 } 142 body, err := json.Marshal(org) 143 Expect(err).To(BeNil()) 144 err = o.addOrganization(body) 145 return err 146 }()).To(BeNil()) 147 }) 148 149 It("test deleteOrganization ", func() { 150 ExpectWithOffset(1, func() error { 151 err := o.deleteOrganization("test_org") 152 return err 153 }()).To(BeNil()) 154 }) 155 }) 156 })