diff --git a/integration/buckets_test.go b/integration/buckets_test.go index 34d008834..2e7fab3fd 100644 --- a/integration/buckets_test.go +++ b/integration/buckets_test.go @@ -158,7 +158,7 @@ func TestMain(m *testing.M) { requestDataBody = bytes.NewReader(requestDataJSON) - // get list of buckets + // delete bucket request, err = http.NewRequest("DELETE", "http://localhost:9090/api/v1/buckets/test1", requestDataBody) if err != nil { log.Println(err) diff --git a/integration/login_test.go b/integration/login_test.go index 8e0ee61ef..522c6682c 100644 --- a/integration/login_test.go +++ b/integration/login_test.go @@ -131,3 +131,33 @@ func TestLogout(t *testing.T) { assert.Nil(err, "Logout errored out") assert.Equal(response.StatusCode, 200) } + +func TestBadLogin(t *testing.T) { + assert := assert.New(t) + + client := &http.Client{ + Timeout: 2 * time.Second, + } + requestData := map[string]string{ + "accessKey": "minioadmin", + "secretKey": "minioadminbad", + } + + requestDataJSON, _ := json.Marshal(requestData) + + requestDataBody := bytes.NewReader(requestDataJSON) + + request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody) + if err != nil { + log.Println(err) + return + } + + request.Header.Add("Content-Type", "application/json") + + response, err := client.Do(request) + + assert.Equal(response.StatusCode, 500, "Login request not rejected") + assert.NotNil(response, "Login response is nil") + assert.Nil(err, "Login errored out") +} diff --git a/integration/user_api_bucket_test.go b/integration/user_api_bucket_test.go index 458fbeb0f..196ade0e7 100644 --- a/integration/user_api_bucket_test.go +++ b/integration/user_api_bucket_test.go @@ -20,6 +20,7 @@ package integration import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -34,6 +35,8 @@ import ( "time" "github.com/minio/console/models" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" "github.com/stretchr/testify/assert" ) @@ -2005,43 +2008,60 @@ func TestDeleteBucket(t *testing.T) { /* Test to delete a bucket */ - - // 1. Create the bucket assert := assert.New(t) - if !BucketGotAdded("testdeletebucket1", false, false, nil, nil, assert, 201) { - return + type args struct { + bucketName string + createBucketName string + } + tests := []struct { + name string + args args + expectedStatus int + }{ + { + name: "Delete a bucket", + expectedStatus: 204, + args: args{ + bucketName: "testdeletebucket1", + createBucketName: "testdeletebucket1", + }, + }, { + name: "Delete invalid bucket", + expectedStatus: 404, + args: args{ + bucketName: "nonexistingbucket", + createBucketName: "", + }, + }, } - // 2. Delete the bucket - deleteBucketResponse, deleteBucketError := DeleteBucket("testdeletebucket1") - assert.Nil(deleteBucketError) - if deleteBucketError != nil { - log.Println(deleteBucketError) - return - } - if deleteBucketResponse != nil { - assert.Equal( - 204, deleteBucketResponse.StatusCode, "Status Code is incorrect") + // Initialize minio client object. + minioClient, err := minio.New("localhost:9000", &minio.Options{ + Creds: credentials.NewStaticV4("minioadmin", "minioadmin", ""), + Secure: false, + }) + if err != nil { + log.Fatalln(err) } - // 3. Verify the bucket is gone by trying to put a tag - tags := make(map[string]string) - tags["tag1"] = "tag1" - putBucketTagResponse, putBucketTagError := PutBucketsTags( - "testdeletebucket1", tags) - if putBucketTagError != nil { - log.Println(putBucketTagError) - assert.Fail("Error adding a tag to the bucket") - return + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create bucket if needed for the test + if tt.args.createBucketName != "" { + if err := minioClient.MakeBucket(context.Background(), tt.args.createBucketName, minio.MakeBucketOptions{}); err != nil { + assert.Failf("Failed to create bucket", "Could not create bucket %s: %v", tt.args.createBucketName, err) + } + } + + // Delete the bucket + deleteBucketResponse, deleteBucketError := DeleteBucket(tt.args.bucketName) + assert.Nil(deleteBucketError) + if deleteBucketResponse != nil { + assert.Equal( + tt.expectedStatus, deleteBucketResponse.StatusCode, "Status Code is incorrect") + } + }) } - finalResponse := inspectHTTPResponse(putBucketTagResponse) - if putBucketTagResponse != nil { - assert.Equal( - 500, putBucketTagResponse.StatusCode, - finalResponse) - } - assert.True( - strings.Contains(finalResponse, "The specified bucket does not exist")) } func TestListBuckets(t *testing.T) { diff --git a/operatorapi/config_test.go b/operatorapi/config_test.go new file mode 100644 index 000000000..9bfcead01 --- /dev/null +++ b/operatorapi/config_test.go @@ -0,0 +1,88 @@ +// This file is part of MinIO Console Server +// Copyright (c) 2022 MinIO, Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package operatorapi + +import ( + "os" + "testing" +) + +func Test_getK8sSAToken(t *testing.T) { + tests := []struct { + name string + want string + envs map[string]string + }{ + { + name: "Missing file, empty", + want: "", + envs: nil, + }, + { + name: "Missing file, return env", + want: "x", + envs: map[string]string{ + ConsoleOperatorSAToken: "x", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.envs != nil { + for k, v := range tt.envs { + os.Setenv(k, v) + } + } + if got := getK8sSAToken(); got != tt.want { + t.Errorf("getK8sSAToken() = %v, want %v", got, tt.want) + } + if tt.envs != nil { + for k := range tt.envs { + os.Unsetenv(k) + } + } + }) + } +} + +func Test_getMarketplace(t *testing.T) { + tests := []struct { + name string + want string + envs map[string]string + }{ + { + name: "Nothing set", + want: "", + envs: nil, + }, + { + name: "Value set", + want: "x", + envs: map[string]string{ + ConsoleMarketplace: "x", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getMarketplace(); got != tt.want { + t.Errorf("getMarketplace() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/restapi/user_buckets.go b/restapi/user_buckets.go index 91cc9a6fb..ca8df5867 100644 --- a/restapi/user_buckets.go +++ b/restapi/user_buckets.go @@ -632,7 +632,12 @@ func getDeleteBucketResponse(session *models.Principal, params bucketApi.DeleteB // defining the client to be used minioClient := minioClient{client: mClient} if err := removeBucket(minioClient, bucketName); err != nil { - return ErrorWithContext(ctx, err) + resp := ErrorWithContext(ctx, err) + errResp := minio.ToErrorResponse(err) + if errResp.Code == "NoSuchBucket" { + resp.Code = 404 + } + return resp } return nil } diff --git a/restapi/utils_test.go b/restapi/utils_test.go index 28046b8d9..cf845b939 100644 --- a/restapi/utils_test.go +++ b/restapi/utils_test.go @@ -214,3 +214,33 @@ func Test_isSafeToPreview(t *testing.T) { }) } } + +func TestRandomCharString(t *testing.T) { + type args struct { + n int + } + tests := []struct { + name string + args args + wantLength int + }{ + { + name: "valid string", + args: args{ + n: 1, + }, + wantLength: 1, + }, { + name: "valid string", + args: args{ + n: 64, + }, + wantLength: 64, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.wantLength, len(RandomCharString(tt.args.n)), "RandomCharString(%v)", tt.args.n) + }) + } +}