You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.3 KiB

4 years ago
  1. package iamapi
  2. import (
  3. "encoding/xml"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/iam"
  7. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  8. "github.com/gorilla/mux"
  9. "github.com/stretchr/testify/assert"
  10. "net/http"
  11. "net/http/httptest"
  12. "testing"
  13. )
  14. var S3config iam_pb.S3ApiConfiguration
  15. var GetS3ApiConfiguration func(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  16. var PutS3ApiConfiguration func(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  17. type iamS3ApiConfigureMock struct{}
  18. func (iam iamS3ApiConfigureMock) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  19. s3cfg = &S3config
  20. return nil
  21. }
  22. func (iam iamS3ApiConfigureMock) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  23. S3config = *s3cfg
  24. return nil
  25. }
  26. var a = IamApiServer{}
  27. func TestCreateUser(t *testing.T) {
  28. userName := aws.String("Test")
  29. params := &iam.CreateUserInput{UserName: userName}
  30. req, _ := iam.New(session.New()).CreateUserRequest(params)
  31. _ = req.Build()
  32. out := CreateUserResponse{}
  33. response, err := executeRequest(req.HTTPRequest, out)
  34. assert.Equal(t, nil, err)
  35. assert.Equal(t, http.StatusOK, response.Code)
  36. //assert.Equal(t, out.XMLName, "lol")
  37. }
  38. func TestListUsers(t *testing.T) {
  39. params := &iam.ListUsersInput{}
  40. req, _ := iam.New(session.New()).ListUsersRequest(params)
  41. _ = req.Build()
  42. out := ListUsersResponse{}
  43. response, err := executeRequest(req.HTTPRequest, out)
  44. assert.Equal(t, nil, err)
  45. assert.Equal(t, http.StatusOK, response.Code)
  46. }
  47. func TestListAccessKeys(t *testing.T) {
  48. svc := iam.New(session.New())
  49. params := &iam.ListAccessKeysInput{}
  50. req, _ := svc.ListAccessKeysRequest(params)
  51. _ = req.Build()
  52. out := ListAccessKeysResponse{}
  53. response, err := executeRequest(req.HTTPRequest, out)
  54. assert.Equal(t, nil, err)
  55. assert.Equal(t, http.StatusOK, response.Code)
  56. }
  57. func TestDeleteUser(t *testing.T) {
  58. userName := aws.String("Test")
  59. params := &iam.DeleteUserInput{UserName: userName}
  60. req, _ := iam.New(session.New()).DeleteUserRequest(params)
  61. _ = req.Build()
  62. out := DeleteUserResponse{}
  63. response, err := executeRequest(req.HTTPRequest, out)
  64. assert.Equal(t, nil, err)
  65. assert.Equal(t, http.StatusNotFound, response.Code)
  66. }
  67. func TestGetUser(t *testing.T) {
  68. userName := aws.String("Test")
  69. params := &iam.GetUserInput{UserName: userName}
  70. req, _ := iam.New(session.New()).GetUserRequest(params)
  71. _ = req.Build()
  72. out := GetUserResponse{}
  73. response, err := executeRequest(req.HTTPRequest, out)
  74. assert.Equal(t, nil, err)
  75. assert.Equal(t, http.StatusNotFound, response.Code)
  76. }
  77. // Todo flat statement
  78. func TestCreatePolicy(t *testing.T) {
  79. params := &iam.CreatePolicyInput{
  80. PolicyName: aws.String("S3-read-only-example-bucket"),
  81. PolicyDocument: aws.String(`
  82. {
  83. "Version": "2012-10-17",
  84. "Statement": [
  85. {
  86. "Effect": "Allow",
  87. "Action": [
  88. "s3:Get*",
  89. "s3:List*"
  90. ],
  91. "Resource": [
  92. "arn:aws:s3:::EXAMPLE-BUCKET",
  93. "arn:aws:s3:::EXAMPLE-BUCKET/*"
  94. ]
  95. }
  96. ]
  97. }`),
  98. }
  99. req, _ := iam.New(session.New()).CreatePolicyRequest(params)
  100. _ = req.Build()
  101. out := CreatePolicyResponse{}
  102. response, err := executeRequest(req.HTTPRequest, out)
  103. assert.Equal(t, nil, err)
  104. assert.Equal(t, http.StatusOK, response.Code)
  105. }
  106. func TestPutUserPolicy(t *testing.T) {
  107. userName := aws.String("Test")
  108. params := &iam.PutUserPolicyInput{
  109. UserName: userName,
  110. PolicyName: aws.String("S3-read-only-example-bucket"),
  111. PolicyDocument: aws.String(
  112. `{
  113. "Version": "2012-10-17",
  114. "Statement": [
  115. {
  116. "Effect": "Allow",
  117. "Action": [
  118. "s3:Get*",
  119. "s3:List*"
  120. ],
  121. "Resource": [
  122. "arn:aws:s3:::EXAMPLE-BUCKET",
  123. "arn:aws:s3:::EXAMPLE-BUCKET/*"
  124. ]
  125. }
  126. ]
  127. }`),
  128. }
  129. req, _ := iam.New(session.New()).PutUserPolicyRequest(params)
  130. _ = req.Build()
  131. out := PutUserPolicyResponse{}
  132. response, err := executeRequest(req.HTTPRequest, out)
  133. assert.Equal(t, nil, err)
  134. assert.Equal(t, http.StatusOK, response.Code)
  135. }
  136. func executeRequest(req *http.Request, v interface{}) (*httptest.ResponseRecorder, error) {
  137. rr := httptest.NewRecorder()
  138. apiRouter := mux.NewRouter().SkipClean(true)
  139. a.s3ApiConfig = iamS3ApiConfigureMock{}
  140. apiRouter.Path("/").Methods("POST").HandlerFunc(a.DoActions)
  141. apiRouter.ServeHTTP(rr, req)
  142. return rr, xml.Unmarshal(rr.Body.Bytes(), &v)
  143. }