-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c721136
commit d071f16
Showing
9 changed files
with
233 additions
and
34 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
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,45 @@ | ||
package gosns | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/Admiral-Piett/goaws/app/models" | ||
"github.com/Admiral-Piett/goaws/app/utils" | ||
|
||
"github.com/Admiral-Piett/goaws/app" | ||
"github.com/Admiral-Piett/goaws/app/interfaces" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func UnsubscribeV1(req *http.Request) (int, interfaces.AbstractResponseBody) { | ||
requestBody := models.NewUnsubscribeRequest() | ||
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false) | ||
if !ok { | ||
log.Error("Invalid Request - UnsubscribeV1") | ||
return utils.CreateErrorResponseV1("InvalidParameterValue", false) | ||
} | ||
|
||
log.Infof("Unsubscribe: %s", requestBody.SubscriptionArn) | ||
for _, topic := range app.SyncTopics.Topics { | ||
for i, sub := range topic.Subscriptions { | ||
if sub.SubscriptionArn == requestBody.SubscriptionArn { | ||
app.SyncTopics.Lock() | ||
|
||
copy(topic.Subscriptions[i:], topic.Subscriptions[i+1:]) | ||
topic.Subscriptions[len(topic.Subscriptions)-1] = nil | ||
topic.Subscriptions = topic.Subscriptions[:len(topic.Subscriptions)-1] | ||
|
||
app.SyncTopics.Unlock() | ||
|
||
respStruct := models.UnsubscribeResponse{ | ||
Xmlns: models.BASE_XMLNS, | ||
Metadata: app.ResponseMetadata{RequestId: uuid.NewString()}, | ||
} | ||
return http.StatusOK, respStruct | ||
} | ||
} | ||
} | ||
return utils.CreateErrorResponseV1("SubscriptionNotFound", false) | ||
} |
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,83 @@ | ||
package gosns | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Admiral-Piett/goaws/app/fixtures" | ||
|
||
"github.com/Admiral-Piett/goaws/app/conf" | ||
|
||
"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/test" | ||
"github.com/Admiral-Piett/goaws/app/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnsubscribeV1_success(t *testing.T) { | ||
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
}() | ||
|
||
subArn := app.SyncTopics.Topics["unit-topic1"].Subscriptions[0].SubscriptionArn | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
v := resultingStruct.(*models.UnsubscribeRequest) | ||
*v = models.UnsubscribeRequest{ | ||
SubscriptionArn: subArn, | ||
} | ||
return true | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
status, response := UnsubscribeV1(r) | ||
|
||
assert.Equal(t, http.StatusOK, status) | ||
_, ok := response.(models.UnsubscribeResponse) | ||
|
||
subs := app.SyncTopics.Topics["unit-topic1"].Subscriptions | ||
assert.Len(t, subs, 0) | ||
assert.True(t, ok) | ||
} | ||
|
||
func TestUnsubscribeV1_invalid_request_body(t *testing.T) { | ||
app.CurrentEnvironment = fixtures.LOCAL_ENVIRONMENT | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
}() | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
return false | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
status, _ := UnsubscribeV1(r) | ||
|
||
assert.Equal(t, http.StatusBadRequest, status) | ||
} | ||
|
||
func TestUnsubscribeV1_invalid_subscription_arn(t *testing.T) { | ||
app.CurrentEnvironment = fixtures.LOCAL_ENVIRONMENT | ||
defer func() { | ||
test.ResetApp() | ||
utils.REQUEST_TRANSFORMER = utils.TransformRequest | ||
}() | ||
|
||
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) { | ||
v := resultingStruct.(*models.UnsubscribeRequest) | ||
*v = models.UnsubscribeRequest{ | ||
SubscriptionArn: "garbage", | ||
} | ||
return true | ||
} | ||
|
||
_, r := test.GenerateRequestInfo("POST", "/", nil, true) | ||
status, _ := UnsubscribeV1(r) | ||
|
||
assert.Equal(t, http.StatusBadRequest, status) | ||
} |
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
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,79 @@ | ||
package smoke_tests | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
|
||
"github.com/Admiral-Piett/goaws/app/conf" | ||
"github.com/Admiral-Piett/goaws/app/test" | ||
|
||
"github.com/gavv/httpexpect/v2" | ||
|
||
"github.com/Admiral-Piett/goaws/app" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sns" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Unsubscribe_json(t *testing.T) { | ||
server := generateServer() | ||
defaultEnv := app.CurrentEnvironment | ||
conf.LoadYamlConfig("../app/conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
server.Close() | ||
test.ResetResources() | ||
app.CurrentEnvironment = defaultEnv | ||
}() | ||
|
||
sdkConfig, _ := config.LoadDefaultConfig(context.TODO()) | ||
sdkConfig.BaseEndpoint = aws.String(server.URL) | ||
snsClient := sns.NewFromConfig(sdkConfig) | ||
|
||
subArn := app.SyncTopics.Topics["unit-topic1"].Subscriptions[0].SubscriptionArn | ||
response, err := snsClient.Unsubscribe(context.TODO(), &sns.UnsubscribeInput{ | ||
SubscriptionArn: &subArn, | ||
}) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
|
||
app.SyncTopics.Lock() | ||
defer app.SyncTopics.Unlock() | ||
|
||
subscriptions := app.SyncTopics.Topics["unit-topic1"].Subscriptions | ||
assert.Len(t, subscriptions, 0) | ||
} | ||
|
||
func Test_Unsubscribe_xml(t *testing.T) { | ||
server := generateServer() | ||
defaultEnv := app.CurrentEnvironment | ||
conf.LoadYamlConfig("../app/conf/mock-data/mock-config.yaml", "BaseUnitTests") | ||
defer func() { | ||
server.Close() | ||
test.ResetResources() | ||
app.CurrentEnvironment = defaultEnv | ||
}() | ||
|
||
e := httpexpect.Default(t, server.URL) | ||
|
||
subArn := app.SyncTopics.Topics["unit-topic1"].Subscriptions[0].SubscriptionArn | ||
requestBody := struct { | ||
Action string `xml:"Action"` | ||
SubscriptionArn string `schema:"SubscriptionArn"` | ||
}{ | ||
Action: "Unsubscribe", | ||
SubscriptionArn: subArn, | ||
} | ||
|
||
e.POST("/"). | ||
WithForm(requestBody). | ||
Expect(). | ||
Status(http.StatusOK). | ||
Body().Raw() | ||
|
||
subscriptions := app.SyncTopics.Topics["unit-topic1"].Subscriptions | ||
assert.Len(t, subscriptions, 0) | ||
} |