github.com/minio/console@v1.3.0/api/admin_arns_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  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"net/http"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/go-openapi/runtime/middleware"
    28  	"github.com/minio/console/api/operations/system"
    29  	"github.com/minio/console/models"
    30  
    31  	"github.com/go-openapi/loads"
    32  	"github.com/minio/console/api/operations"
    33  	"github.com/minio/madmin-go/v3"
    34  
    35  	asrt "github.com/stretchr/testify/assert"
    36  )
    37  
    38  func TestArnsList(t *testing.T) {
    39  	assert := asrt.New(t)
    40  	adminClient := AdminClientMock{}
    41  	// Test-1 : getArns() returns proper arn list
    42  	MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) {
    43  		return madmin.InfoMessage{
    44  			SQSARN: []string{"uno"},
    45  		}, nil
    46  	}
    47  	ctx, cancel := context.WithCancel(context.Background())
    48  	defer cancel()
    49  	arnsList, err := getArns(ctx, adminClient)
    50  	assert.NotNil(arnsList, "arn list was returned nil")
    51  	if arnsList != nil {
    52  		assert.Equal(len(arnsList.Arns), 1, "Incorrect arns count")
    53  	}
    54  	assert.Nil(err, "Error should have been nil")
    55  
    56  	// Test-2 : getArns(ctx) fails for whatever reason
    57  	MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) {
    58  		return madmin.InfoMessage{}, errors.New("some reason")
    59  	}
    60  
    61  	arnsList, err = getArns(ctx, adminClient)
    62  	assert.Nil(arnsList, "arn list was not returned nil")
    63  	assert.NotNil(err, "An error should have been returned")
    64  }
    65  
    66  func TestRegisterAdminArnsHandlers(t *testing.T) {
    67  	assert := asrt.New(t)
    68  	swaggerSpec, err := loads.Embedded(SwaggerJSON, FlatSwaggerJSON)
    69  	if err != nil {
    70  		assert.Fail("Error")
    71  	}
    72  	api := operations.NewConsoleAPI(swaggerSpec)
    73  	api.SystemArnListHandler = nil
    74  	registerAdminArnsHandlers(api)
    75  	if api.SystemArnListHandler == nil {
    76  		assert.Fail("Assignment should happen")
    77  	} else {
    78  		fmt.Println("Function got assigned: ", api.SystemArnListHandler)
    79  	}
    80  
    81  	// To test error case in registerAdminArnsHandlers
    82  	request, _ := http.NewRequest(
    83  		"GET",
    84  		"http://localhost:9090/api/v1/buckets/",
    85  		nil,
    86  	)
    87  	ArnListParamsStruct := system.ArnListParams{
    88  		HTTPRequest: request,
    89  	}
    90  	modelsPrincipal := models.Principal{
    91  		STSAccessKeyID: "accesskey",
    92  	}
    93  	var value middleware.Responder = api.SystemArnListHandler.Handle(ArnListParamsStruct, &modelsPrincipal)
    94  	str := fmt.Sprintf("%#v", value)
    95  	fmt.Println("value: ", str)
    96  	assert.Equal(strings.Contains(str, "_statusCode:500"), true)
    97  }