Skip to content

Commit f7595ad

Browse files
NRatSonyDai.Otsuka
authored and
Dai.Otsuka
committed
add sns list subcriptions support
add bad request ut case to listsubscriptionsv1 rename listSubscriptions test case review ref
1 parent 2ca8cc6 commit f7595ad

File tree

10 files changed

+393
-106
lines changed

10 files changed

+393
-106
lines changed

app/gosns/gosns.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,6 @@ func ConfirmSubscription(w http.ResponseWriter, req *http.Request) {
146146

147147
}
148148

149-
func ListSubscriptions(w http.ResponseWriter, req *http.Request) {
150-
content := req.FormValue("ContentType")
151-
152-
uuid, _ := common.NewUUID()
153-
respStruct := app.ListSubscriptionsResponse{}
154-
respStruct.Xmlns = "http://queue.amazonaws.com/doc/2012-11-05/"
155-
respStruct.Metadata.RequestId = uuid
156-
respStruct.Result.Subscriptions.Member = make([]app.TopicMemberResult, 0, 0)
157-
158-
for _, topic := range app.SyncTopics.Topics {
159-
for _, sub := range topic.Subscriptions {
160-
tar := app.TopicMemberResult{TopicArn: topic.Arn, Protocol: sub.Protocol,
161-
SubscriptionArn: sub.SubscriptionArn, Endpoint: sub.EndPoint, Owner: app.CurrentEnvironment.AccountID}
162-
respStruct.Result.Subscriptions.Member = append(respStruct.Result.Subscriptions.Member, tar)
163-
}
164-
}
165-
166-
SendResponseBack(w, req, respStruct, content)
167-
}
168-
169149
func ListSubscriptionsByTopic(w http.ResponseWriter, req *http.Request) {
170150
content := req.FormValue("ContentType")
171151
topicArn := req.FormValue("TopicArn")

app/gosns/gosns_test.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -74,65 +74,6 @@ func TestListSubscriptionByTopicResponse_No_Owner(t *testing.T) {
7474
}
7575
}
7676

77-
func TestListSubscriptionsResponse_No_Owner(t *testing.T) {
78-
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "Local")
79-
defer func() {
80-
test.ResetApp()
81-
}()
82-
83-
// set accountID to test value so it can be populated in response
84-
app.CurrentEnvironment.AccountID = "100010001000"
85-
86-
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
87-
// pass 'nil' as the third parameter.
88-
req, err := http.NewRequest("POST", "/", nil)
89-
if err != nil {
90-
t.Fatal(err)
91-
}
92-
93-
form := url.Values{}
94-
form.Add("TopicArn", "arn:aws:sns:local:000000000000:local-topic1")
95-
req.PostForm = form
96-
97-
// Prepare existant topic
98-
topic := &app.Topic{
99-
Name: "UnitTestTopic1",
100-
Arn: "arn:aws:sns:local:100010001000:UnitTestTopic1",
101-
Subscriptions: []*app.Subscription{
102-
{
103-
TopicArn: "",
104-
Protocol: "",
105-
SubscriptionArn: "",
106-
EndPoint: "",
107-
Raw: false,
108-
FilterPolicy: &app.FilterPolicy{},
109-
},
110-
},
111-
}
112-
app.SyncTopics.Topics["UnitTestTopic1"] = topic
113-
114-
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
115-
rr := httptest.NewRecorder()
116-
handler := http.HandlerFunc(ListSubscriptions)
117-
118-
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
119-
// directly and pass in our Request and ResponseRecorder.
120-
handler.ServeHTTP(rr, req)
121-
122-
// Check the status code is what we expect.
123-
if status := rr.Code; status != http.StatusOK {
124-
t.Errorf("handler returned wrong status code: got %v want %v",
125-
status, http.StatusOK)
126-
}
127-
128-
// Check the response body is what we expect.
129-
expected := `<Owner>` + app.CurrentEnvironment.AccountID + `</Owner>`
130-
if !strings.Contains(rr.Body.String(), expected) {
131-
t.Errorf("handler returned empty owner for subscription member: got %v want %v",
132-
rr.Body.String(), expected)
133-
}
134-
}
135-
13677
func TestGetSubscriptionAttributesHandler_POST_Success(t *testing.T) {
13778
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
13879
// pass 'nil' as the third parameter.

app/gosns/list_subscriptions.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gosns
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/google/uuid"
7+
8+
"github.com/Admiral-Piett/goaws/app"
9+
"github.com/Admiral-Piett/goaws/app/models"
10+
"github.com/Admiral-Piett/goaws/app/utils"
11+
12+
"github.com/Admiral-Piett/goaws/app/interfaces"
13+
log "github.com/sirupsen/logrus"
14+
)
15+
16+
func ListSubscriptionsV1(req *http.Request) (int, interfaces.AbstractResponseBody) {
17+
requestBody := models.NewListSubscriptionsRequest()
18+
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false)
19+
if !ok {
20+
log.Error("Invalid Request - ListSubscriptionsV1")
21+
return utils.CreateErrorResponseV1("InvalidParameterValue", false)
22+
}
23+
24+
log.Debug("Listing Subscriptions")
25+
requestId := uuid.NewString()
26+
respStruct := models.ListSubscriptionsResponse{}
27+
respStruct.Xmlns = models.BASE_XMLNS
28+
respStruct.Metadata.RequestId = requestId
29+
respStruct.Result.Subscriptions.Member = make([]models.TopicMemberResult, 0)
30+
31+
for _, topic := range app.SyncTopics.Topics {
32+
for _, sub := range topic.Subscriptions {
33+
tar := models.TopicMemberResult{TopicArn: topic.Arn, Protocol: sub.Protocol,
34+
SubscriptionArn: sub.SubscriptionArn, Endpoint: sub.EndPoint, Owner: app.CurrentEnvironment.AccountID}
35+
respStruct.Result.Subscriptions.Member = append(respStruct.Result.Subscriptions.Member, tar)
36+
}
37+
}
38+
39+
return http.StatusOK, respStruct
40+
}

app/gosns/list_subscriptions_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package gosns
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/Admiral-Piett/goaws/app/conf"
8+
"github.com/Admiral-Piett/goaws/app/interfaces"
9+
"github.com/Admiral-Piett/goaws/app/models"
10+
"github.com/Admiral-Piett/goaws/app/test"
11+
"github.com/Admiral-Piett/goaws/app/utils"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestListSubcriptionsV1_NoSubscriptions(t *testing.T) {
16+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "NoQueuesOrTopics")
17+
defer func() {
18+
test.ResetApp()
19+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
20+
}()
21+
22+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
23+
v := resultingStruct.(*models.ListSubscriptionsRequest)
24+
*v = models.ListSubscriptionsRequest{
25+
NextToken: "",
26+
}
27+
return true
28+
}
29+
30+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
31+
code, res := ListSubscriptionsV1(r)
32+
33+
response, _ := res.(models.ListSubscriptionsResponse)
34+
35+
assert.Equal(t, http.StatusOK, code)
36+
assert.Equal(t, models.BASE_XMLNS, response.Xmlns)
37+
assert.NotEqual(t, "", response.Metadata)
38+
39+
assert.Len(t, response.Result.Subscriptions.Member, 0)
40+
}
41+
42+
func TestListSubcriptionsV1_MultipleSubscriptions(t *testing.T) {
43+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "Local")
44+
defer func() {
45+
test.ResetApp()
46+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
47+
}()
48+
49+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
50+
v := resultingStruct.(*models.ListSubscriptionsRequest)
51+
*v = models.ListSubscriptionsRequest{
52+
NextToken: "",
53+
}
54+
return true
55+
}
56+
57+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
58+
code, res := ListSubscriptionsV1(r)
59+
60+
response, _ := res.(models.ListSubscriptionsResponse)
61+
62+
assert.Equal(t, http.StatusOK, code)
63+
assert.Equal(t, models.BASE_XMLNS, response.Xmlns)
64+
assert.NotEqual(t, "", response.Metadata)
65+
66+
assert.Len(t, response.Result.Subscriptions.Member, 2)
67+
assert.NotEqual(t, response.Result.Subscriptions.Member[0].SubscriptionArn, response.Result.Subscriptions.Member[1].SubscriptionArn)
68+
}
69+
70+
func TestListSubscriptionsV1_request_transformer_error(t *testing.T) {
71+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
72+
defer func() {
73+
test.ResetApp()
74+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
75+
}()
76+
77+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
78+
return false
79+
}
80+
81+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
82+
code, _ := ListSubscriptionsV1(r)
83+
84+
assert.Equal(t, http.StatusBadRequest, code)
85+
}

app/models/responses.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,35 @@ func (r DeleteTopicResponse) GetResult() interface{} {
461461
func (r DeleteTopicResponse) GetRequestId() string {
462462
return r.Metadata.RequestId
463463
}
464+
465+
/** List Subcriptions **/
466+
467+
type TopicMemberResult struct {
468+
TopicArn string `xml:"TopicArn"`
469+
Protocol string `xml:"Protocol"`
470+
SubscriptionArn string `xml:"SubscriptionArn"`
471+
Owner string `xml:"Owner"`
472+
Endpoint string `xml:"Endpoint"`
473+
}
474+
475+
type TopicSubscriptions struct {
476+
Member []TopicMemberResult `xml:"member"`
477+
}
478+
479+
type ListSubscriptionsResult struct {
480+
Subscriptions TopicSubscriptions `xml:"Subscriptions"`
481+
}
482+
483+
type ListSubscriptionsResponse struct {
484+
Xmlns string `xml:"xmlns,attr"`
485+
Result ListSubscriptionsResult `xml:"ListSubscriptionsResult"`
486+
Metadata app.ResponseMetadata `xml:"ResponseMetadata"`
487+
}
488+
489+
func (r ListSubscriptionsResponse) GetResult() interface{} {
490+
return r.Result
491+
}
492+
493+
func (r ListSubscriptionsResponse) GetRequestId() string {
494+
return r.Metadata.RequestId
495+
}

app/models/sns.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,15 @@ type DeleteTopicRequest struct {
248248
}
249249

250250
func (r *DeleteTopicRequest) SetAttributesFromForm(values url.Values) {}
251+
252+
// ListSubscriptionsV1
253+
254+
func NewListSubscriptionsRequest() *ListSubscriptionsRequest {
255+
return &ListSubscriptionsRequest{}
256+
}
257+
258+
type ListSubscriptionsRequest struct {
259+
NextToken string `json:"NextToken" schema:"NextToken"` // not implemented
260+
}
261+
262+
func (r *ListSubscriptionsRequest) SetAttributesFromForm(values url.Values) {}

app/router/router.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ var routingTableV1 = map[string]func(r *http.Request) (int, interfaces.AbstractR
7979
"DeleteMessageBatch": sqs.DeleteMessageBatchV1,
8080

8181
// SNS
82-
"Subscribe": sns.SubscribeV1,
83-
"Unsubscribe": sns.UnsubscribeV1,
84-
"Publish": sns.PublishV1,
85-
"ListTopics": sns.ListTopicsV1,
86-
"CreateTopic": sns.CreateTopicV1,
87-
"DeleteTopic": sns.DeleteTopicV1,
82+
"Subscribe": sns.SubscribeV1,
83+
"Unsubscribe": sns.UnsubscribeV1,
84+
"Publish": sns.PublishV1,
85+
"ListTopics": sns.ListTopicsV1,
86+
"CreateTopic": sns.CreateTopicV1,
87+
"DeleteTopic": sns.DeleteTopicV1,
88+
"ListSubscriptions": sns.ListSubscriptionsV1,
8889
}
8990

9091
var routingTable = map[string]http.HandlerFunc{
9192
// SNS
9293
"SetSubscriptionAttributes": sns.SetSubscriptionAttributes,
9394
"GetSubscriptionAttributes": sns.GetSubscriptionAttributes,
9495
"ListSubscriptionsByTopic": sns.ListSubscriptionsByTopic,
95-
"ListSubscriptions": sns.ListSubscriptions,
9696

9797
// SNS Internal
9898
"ConfirmSubscription": sns.ConfirmSubscription,

app/router/router_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,20 +271,20 @@ func TestActionHandler_v0_xml(t *testing.T) {
271271
"DeleteMessageBatch": sqs.DeleteMessageBatchV1,
272272

273273
// SNS
274-
"Subscribe": sns.SubscribeV1,
275-
"Unsubscribe": sns.UnsubscribeV1,
276-
"Publish": sns.PublishV1,
277-
"ListTopics": sns.ListTopicsV1,
278-
"CreateTopic": sns.CreateTopicV1,
279-
"DeleteTopic": sns.DeleteTopicV1,
274+
"Subscribe": sns.SubscribeV1,
275+
"Unsubscribe": sns.UnsubscribeV1,
276+
"Publish": sns.PublishV1,
277+
"ListTopics": sns.ListTopicsV1,
278+
"CreateTopic": sns.CreateTopicV1,
279+
"DeleteTopic": sns.DeleteTopicV1,
280+
"ListSubscriptions": sns.ListSubscriptionsV1,
280281
}
281282

282283
routingTable = map[string]http.HandlerFunc{
283284
// SNS
284285
"SetSubscriptionAttributes": sns.SetSubscriptionAttributes,
285286
"GetSubscriptionAttributes": sns.GetSubscriptionAttributes,
286287
"ListSubscriptionsByTopic": sns.ListSubscriptionsByTopic,
287-
"ListSubscriptions": sns.ListSubscriptions,
288288

289289
// SNS Internal
290290
"ConfirmSubscription": sns.ConfirmSubscription,

app/sns_messages.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type GetSubscriptionAttributesResponse struct {
2727
Metadata ResponseMetadata `xml:"ResponseMetadata,omitempty"`
2828
}
2929

30-
/*** List Subscriptions Response */
30+
/*** List Subscriptions By Topic Response */
31+
3132
type TopicMemberResult struct {
3233
TopicArn string `xml:"TopicArn"`
3334
Protocol string `xml:"Protocol"`
@@ -40,18 +41,6 @@ type TopicSubscriptions struct {
4041
Member []TopicMemberResult `xml:"member"`
4142
}
4243

43-
type ListSubscriptionsResult struct {
44-
Subscriptions TopicSubscriptions `xml:"Subscriptions"`
45-
}
46-
47-
type ListSubscriptionsResponse struct {
48-
Xmlns string `xml:"xmlns,attr"`
49-
Result ListSubscriptionsResult `xml:"ListSubscriptionsResult"`
50-
Metadata ResponseMetadata `xml:"ResponseMetadata"`
51-
}
52-
53-
/*** List Subscriptions By Topic Response */
54-
5544
type ListSubscriptionsByTopicResult struct {
5645
Subscriptions TopicSubscriptions `xml:"Subscriptions"`
5746
}

0 commit comments

Comments
 (0)