2018-08-26 19:15:48 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-04-11 20:29:04 +08:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
adapter "github.com/mainflux/mainflux/http"
|
|
|
|
"github.com/mainflux/mainflux/http/api"
|
|
|
|
"github.com/mainflux/mainflux/http/mocks"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newService() mainflux.MessagePublisher {
|
|
|
|
pub := mocks.NewPublisher()
|
|
|
|
return adapter.New(pub)
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:13:09 +08:00
|
|
|
func newHTTPServer(pub mainflux.MessagePublisher, cc mainflux.ThingsServiceClient) *httptest.Server {
|
2018-05-11 05:53:25 +08:00
|
|
|
mux := api.MakeHandler(pub, cc)
|
2018-04-11 20:29:04 +08:00
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testRequest struct {
|
|
|
|
client *http.Client
|
|
|
|
method string
|
|
|
|
url string
|
|
|
|
contentType string
|
|
|
|
token string
|
|
|
|
body io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr testRequest) make() (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(tr.method, tr.url, tr.body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tr.token != "" {
|
|
|
|
req.Header.Set("Authorization", tr.token)
|
|
|
|
}
|
|
|
|
if tr.contentType != "" {
|
|
|
|
req.Header.Set("Content-Type", tr.contentType)
|
|
|
|
}
|
|
|
|
return tr.client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublish(t *testing.T) {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID := "1"
|
2018-05-21 18:51:46 +08:00
|
|
|
contentType := "application/senml+json"
|
|
|
|
token := "auth_token"
|
|
|
|
invalidToken := "invalid_token"
|
|
|
|
msg := `[{"n":"current","t":-1,"v":1.6}]`
|
2018-12-05 20:09:25 +08:00
|
|
|
thingsClient := mocks.NewThingsClient(map[string]string{token: chanID})
|
2018-04-11 20:29:04 +08:00
|
|
|
pub := newService()
|
2018-05-15 23:13:09 +08:00
|
|
|
ts := newHTTPServer(pub, thingsClient)
|
2018-04-11 20:29:04 +08:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID string
|
2018-04-11 20:29:04 +08:00
|
|
|
msg string
|
|
|
|
contentType string
|
|
|
|
auth string
|
|
|
|
status int
|
|
|
|
}{
|
2018-05-21 18:51:46 +08:00
|
|
|
"publish message": {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID: chanID,
|
2018-05-21 18:51:46 +08:00
|
|
|
msg: msg,
|
|
|
|
contentType: contentType,
|
|
|
|
auth: token,
|
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
|
|
|
"publish message without authorization token": {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID: chanID,
|
2018-05-21 18:51:46 +08:00
|
|
|
msg: msg,
|
|
|
|
contentType: contentType,
|
|
|
|
auth: "",
|
|
|
|
status: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
"publish message with invalid authorization token": {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID: chanID,
|
2018-05-21 18:51:46 +08:00
|
|
|
msg: msg,
|
|
|
|
contentType: contentType,
|
|
|
|
auth: invalidToken,
|
|
|
|
status: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
"publish message without content type": {
|
2018-06-16 08:30:46 +08:00
|
|
|
chanID: chanID,
|
2018-05-21 18:51:46 +08:00
|
|
|
msg: msg,
|
|
|
|
contentType: "",
|
|
|
|
auth: token,
|
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
2018-12-05 20:09:25 +08:00
|
|
|
"publish message to invalid channel": {
|
|
|
|
chanID: "",
|
2018-06-16 08:30:46 +08:00
|
|
|
msg: msg,
|
|
|
|
contentType: contentType,
|
|
|
|
auth: token,
|
2018-12-05 20:09:25 +08:00
|
|
|
status: http.StatusBadRequest,
|
2018-06-16 08:30:46 +08:00
|
|
|
},
|
|
|
|
"publish message unable to authorize": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msg,
|
|
|
|
contentType: contentType,
|
|
|
|
auth: mocks.ServiceErrToken,
|
|
|
|
status: http.StatusServiceUnavailable,
|
|
|
|
},
|
2018-04-11 20:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
req := testRequest{
|
2018-05-15 23:13:09 +08:00
|
|
|
client: ts.Client(),
|
2018-04-11 20:29:04 +08:00
|
|
|
method: http.MethodPost,
|
2018-06-16 08:30:46 +08:00
|
|
|
url: fmt.Sprintf("%s/channels/%s/messages", ts.URL, tc.chanID),
|
2018-04-11 20:29:04 +08:00
|
|
|
contentType: tc.contentType,
|
|
|
|
token: tc.auth,
|
|
|
|
body: strings.NewReader(tc.msg),
|
|
|
|
}
|
|
|
|
res, err := req.make()
|
|
|
|
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
|
|
|
|
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", desc, tc.status, res.StatusCode))
|
|
|
|
}
|
|
|
|
}
|