-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
198 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gosns | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Admiral-Piett/goaws/app" | ||
"github.com/Admiral-Piett/goaws/app/interfaces" | ||
"github.com/Admiral-Piett/goaws/app/models" | ||
"github.com/Admiral-Piett/goaws/app/utils" | ||
"github.com/google/uuid" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func ConfirmSubscriptionV1(req *http.Request) (int, interfaces.AbstractResponseBody) { | ||
requestBody := models.NewConfirmSubscriptionRequest() | ||
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false) | ||
if !ok { | ||
log.Error("Invalid Request - ConfirmSubscriptionV1") | ||
return utils.CreateErrorResponseV1("InvalidParameterValue", false) | ||
} | ||
topicArn := requestBody.TopicArn | ||
confirmToken := requestBody.Token | ||
var pendingConfirm pendingConfirm | ||
|
||
if pending, ok := TOPIC_DATA[topicArn]; !ok { | ||
return utils.CreateErrorResponseV1("SubscriptionNotFound", false) | ||
} else { | ||
pendingConfirm = *pending | ||
} | ||
|
||
if pendingConfirm.token != confirmToken { | ||
return utils.CreateErrorResponseV1("SubscriptionNotFound", false) | ||
} | ||
respStruct := models.ConfirmSubscriptionResponse{ | ||
Xmlns: models.BASE_XMLNS, | ||
Result: models.ConfirmSubscriptionResult{SubscriptionArn: pendingConfirm.subArn}, | ||
Metadata: app.ResponseMetadata{RequestId: uuid.NewString()}, | ||
} | ||
return http.StatusOK, respStruct | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package gosns | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Admiral-Piett/goaws/app/conf" | ||
"github.com/Admiral-Piett/goaws/app/interfaces" | ||
"github.com/Admiral-Piett/goaws/app/models" | ||
"github.com/Admiral-Piett/goaws/app/test" | ||
"github.com/Admiral-Piett/goaws/app/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfirmSubscriptionV1_Success(t *testing.T) { | ||
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
TOPIC_DATA = make(map[string]*pendingConfirm) | ||
}() | ||
|
||
topicArn := "test-topic-arn" | ||
confirmToken := "test-token" | ||
subscriptionArn := "test-sub-arn" | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
v := resultingStruct.(*models.ConfirmSubscriptionRequest) | ||
*v = models.ConfirmSubscriptionRequest{ | ||
TopicArn: topicArn, | ||
Token: confirmToken, | ||
} | ||
return true | ||
} | ||
// set pending subscription | ||
TOPIC_DATA[topicArn] = &pendingConfirm{ | ||
subArn: subscriptionArn, | ||
token: confirmToken, | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
code, response := ConfirmSubscriptionV1(r) | ||
|
||
result := response.GetResult().(models.ConfirmSubscriptionResult) | ||
assert.Equal(t, http.StatusOK, code) | ||
assert.Equal(t, subscriptionArn, result.SubscriptionArn) | ||
} | ||
|
||
func TestConfirmSubscriptionV1_NotFoundSubscription(t *testing.T) { | ||
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "NoQueuesOrTopics") | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
TOPIC_DATA = make(map[string]*pendingConfirm) | ||
}() | ||
|
||
topicArn := "test-topic-arn" | ||
confirmToken := "test-token" | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
v := resultingStruct.(*models.ConfirmSubscriptionRequest) | ||
*v = models.ConfirmSubscriptionRequest{ | ||
TopicArn: topicArn, | ||
Token: confirmToken, | ||
} | ||
return true | ||
} | ||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
code, response := ConfirmSubscriptionV1(r) | ||
result := response.GetResult().(models.ErrorResult) | ||
assert.Equal(t, http.StatusNotFound, code) | ||
assert.Contains(t, result.Message, "The specified subscription does not exist for this wsdl version.") | ||
} | ||
|
||
func TestConfirmSubscriptionV1_MismatchToken(t *testing.T) { | ||
|
||
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
TOPIC_DATA = make(map[string]*pendingConfirm) | ||
}() | ||
|
||
topicArn := "test-topic-arn" | ||
confirmToken := "test-token" | ||
subscriptionArn := "test-sub-arn" | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
v := resultingStruct.(*models.ConfirmSubscriptionRequest) | ||
*v = models.ConfirmSubscriptionRequest{ | ||
TopicArn: topicArn, | ||
Token: "dummy", | ||
} | ||
return true | ||
} | ||
|
||
// set dummy subscription | ||
TOPIC_DATA[topicArn] = &pendingConfirm{ | ||
subArn: subscriptionArn, | ||
token: confirmToken, | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
code, response := ConfirmSubscriptionV1(r) | ||
result := response.GetResult().(models.ErrorResult) | ||
assert.Equal(t, http.StatusNotFound, code) | ||
assert.Contains(t, result.Message, "The specified subscription does not exist for this wsdl version.") | ||
} | ||
|
||
func TestConfirmSubscriptionV1_TransformerError(t *testing.T) { | ||
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
TOPIC_DATA = make(map[string]*pendingConfirm) | ||
}() | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
return false | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
code, _ := ConfirmSubscriptionV1(r) | ||
assert.Equal(t, http.StatusBadRequest, code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters