diff options
| author | Tom Crasset <25140344+tcrasset@users.noreply.github.com> | 2025-01-16 17:23:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-16 08:23:19 -0800 |
| commit | aa299462f2c4ea857ee6997ec25eedd812904212 (patch) | |
| tree | 16f20137db23a36262c8c4025dda2b7f1ed4848c /weed/iamapi/iamapi_test.go | |
| parent | 2304d2b4728a6860865aac1f976f3faef493bfe0 (diff) | |
| download | seaweedfs-aa299462f2c4ea857ee6997ec25eedd812904212.tar.xz seaweedfs-aa299462f2c4ea857ee6997ec25eedd812904212.zip | |
improve iam error handling (#6446)
* improve iam error handling
* Delete docker/test.py
Diffstat (limited to 'weed/iamapi/iamapi_test.go')
| -rw-r--r-- | weed/iamapi/iamapi_test.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/weed/iamapi/iamapi_test.go b/weed/iamapi/iamapi_test.go index f32e1ac51..067209eb5 100644 --- a/weed/iamapi/iamapi_test.go +++ b/weed/iamapi/iamapi_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -152,6 +153,53 @@ func TestPutUserPolicy(t *testing.T) { assert.Equal(t, http.StatusOK, response.Code) } +func TestPutUserPolicyError(t *testing.T) { + userName := aws.String("InvalidUser") + params := &iam.PutUserPolicyInput{ + UserName: userName, + PolicyName: aws.String("S3-read-only-example-bucket"), + PolicyDocument: aws.String( + `{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:Get*", + "s3:List*" + ], + "Resource": [ + "arn:aws:s3:::EXAMPLE-BUCKET", + "arn:aws:s3:::EXAMPLE-BUCKET/*" + ] + } + ] + }`), + } + req, _ := iam.New(session.New()).PutUserPolicyRequest(params) + _ = req.Build() + response, err := executeRequest(req.HTTPRequest, nil) + assert.Equal(t, nil, err) + assert.Equal(t, http.StatusNotFound, response.Code) + + expectedMessage := "the user with name InvalidUser cannot be found" + expectedCode := "NoSuchEntity" + + code, message := extractErrorCodeAndMessage(response) + + assert.Equal(t, expectedMessage, message) + assert.Equal(t, expectedCode, code) +} + +func extractErrorCodeAndMessage(response *httptest.ResponseRecorder) (string, string) { + pattern := `<Error><Code>(.*)</Code><Message>(.*)</Message><Type>(.*)</Type></Error>` + re := regexp.MustCompile(pattern) + + code := re.FindStringSubmatch(response.Body.String())[1] + message := re.FindStringSubmatch(response.Body.String())[2] + return code, message +} + func TestGetUserPolicy(t *testing.T) { userName := aws.String("Test") params := &iam.GetUserPolicyInput{UserName: userName, PolicyName: aws.String("S3-read-only-example-bucket")} |
