2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-04-11 14:29:04 +02:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
|
|
|
2018-04-11 14:29:04 +02:00
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
adapter "github.com/mainflux/mainflux/http"
|
|
|
|
"github.com/mainflux/mainflux/http/api"
|
|
|
|
"github.com/mainflux/mainflux/http/mocks"
|
2022-03-03 17:13:46 +01:00
|
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
|
|
|
"github.com/mainflux/mainflux/logger"
|
2018-04-11 14:29:04 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-04-01 21:22:13 +02:00
|
|
|
func newService(cc mainflux.ThingsServiceClient) adapter.Service {
|
2018-04-11 14:29:04 +02:00
|
|
|
pub := mocks.NewPublisher()
|
2019-07-18 15:01:09 +02:00
|
|
|
return adapter.New(pub, cc)
|
2018-04-11 14:29:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 21:22:13 +02:00
|
|
|
func newHTTPServer(svc adapter.Service) *httptest.Server {
|
2022-03-03 17:13:46 +01:00
|
|
|
logger := logger.NewMock()
|
|
|
|
mux := api.MakeHandler(svc, mocktracer.New(), logger)
|
2018-04-11 14:29:04 +02:00
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testRequest struct {
|
|
|
|
client *http.Client
|
|
|
|
method string
|
|
|
|
url string
|
|
|
|
contentType string
|
|
|
|
token string
|
|
|
|
body io.Reader
|
2021-07-22 15:47:55 +02:00
|
|
|
basicAuth bool
|
2018-04-11 14:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr testRequest) make() (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest(tr.method, tr.url, tr.body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-22 15:47:55 +02:00
|
|
|
|
2018-04-11 14:29:04 +02:00
|
|
|
if tr.token != "" {
|
2022-03-06 01:49:34 +01:00
|
|
|
req.Header.Set("Authorization", apiutil.ThingPrefix+tr.token)
|
2018-04-11 14:29:04 +02:00
|
|
|
}
|
2021-07-22 15:47:55 +02:00
|
|
|
if tr.basicAuth && tr.token != "" {
|
|
|
|
req.SetBasicAuth("", tr.token)
|
|
|
|
}
|
2018-04-11 14:29:04 +02:00
|
|
|
if tr.contentType != "" {
|
|
|
|
req.Header.Set("Content-Type", tr.contentType)
|
|
|
|
}
|
|
|
|
return tr.client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublish(t *testing.T) {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID := "1"
|
2022-06-13 12:04:48 +02:00
|
|
|
ctSenmlJSON := "application/senml+json"
|
|
|
|
ctSenmlCBOR := "application/senml+cbor"
|
|
|
|
ctJSON := "application/json"
|
2022-02-14 22:49:23 +01:00
|
|
|
thingKey := "thing_key"
|
|
|
|
invalidKey := "invalid_key"
|
2018-05-21 12:51:46 +02:00
|
|
|
msg := `[{"n":"current","t":-1,"v":1.6}]`
|
2022-06-13 12:04:48 +02:00
|
|
|
msgJSON := `{"field1":"val1","field2":"val2"}`
|
|
|
|
msgCBOR := `81A3616E6763757272656E746174206176FB3FF999999999999A`
|
2022-02-14 22:49:23 +01:00
|
|
|
thingsClient := mocks.NewThingsClient(map[string]string{thingKey: chanID})
|
2020-04-01 21:22:13 +02:00
|
|
|
svc := newService(thingsClient)
|
|
|
|
ts := newHTTPServer(svc)
|
2018-04-11 14:29:04 +02:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID string
|
2018-04-11 14:29:04 +02:00
|
|
|
msg string
|
|
|
|
contentType string
|
2022-02-14 22:49:23 +01:00
|
|
|
key string
|
2018-04-11 14:29:04 +02:00
|
|
|
status int
|
2021-07-22 15:47:55 +02:00
|
|
|
basicAuth bool
|
2018-04-11 14:29:04 +02:00
|
|
|
}{
|
2018-05-21 12:51:46 +02:00
|
|
|
"publish message": {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID: chanID,
|
2018-05-21 12:51:46 +02:00
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
|
|
|
key: thingKey,
|
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
|
|
|
"publish message with application/senml+cbor content-type": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msgCBOR,
|
|
|
|
contentType: ctSenmlCBOR,
|
|
|
|
key: thingKey,
|
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
|
|
|
"publish message with application/json content-type": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msgJSON,
|
|
|
|
contentType: ctJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: thingKey,
|
2018-05-21 12:51:46 +02:00
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
2022-02-14 22:49:23 +01:00
|
|
|
"publish message with empty key": {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID: chanID,
|
2018-05-21 12:51:46 +02:00
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: "",
|
2022-02-01 17:33:23 +01:00
|
|
|
status: http.StatusUnauthorized,
|
2018-05-21 12:51:46 +02:00
|
|
|
},
|
2021-07-22 15:47:55 +02:00
|
|
|
"publish message with basic auth": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: thingKey,
|
2021-07-22 15:47:55 +02:00
|
|
|
basicAuth: true,
|
|
|
|
status: http.StatusAccepted,
|
|
|
|
},
|
2022-02-14 22:49:23 +01:00
|
|
|
"publish message with invalid key": {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID: chanID,
|
2018-05-21 12:51:46 +02:00
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: invalidKey,
|
2022-02-01 17:33:23 +01:00
|
|
|
status: http.StatusUnauthorized,
|
2018-05-21 12:51:46 +02:00
|
|
|
},
|
2021-07-22 15:47:55 +02:00
|
|
|
"publish message with invalid basic auth": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: invalidKey,
|
2021-07-22 15:47:55 +02:00
|
|
|
basicAuth: true,
|
2022-02-01 17:33:23 +01:00
|
|
|
status: http.StatusUnauthorized,
|
2021-07-22 15:47:55 +02:00
|
|
|
},
|
2018-05-21 12:51:46 +02:00
|
|
|
"publish message without content type": {
|
2018-06-16 02:30:46 +02:00
|
|
|
chanID: chanID,
|
2018-05-21 12:51:46 +02:00
|
|
|
msg: msg,
|
|
|
|
contentType: "",
|
2022-02-14 22:49:23 +01:00
|
|
|
key: thingKey,
|
|
|
|
status: http.StatusUnsupportedMediaType,
|
2018-05-21 12:51:46 +02:00
|
|
|
},
|
2018-12-05 13:09:25 +01:00
|
|
|
"publish message to invalid channel": {
|
|
|
|
chanID: "",
|
2018-06-16 02:30:46 +02:00
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: thingKey,
|
2018-12-05 13:09:25 +01:00
|
|
|
status: http.StatusBadRequest,
|
2018-06-16 02:30:46 +02:00
|
|
|
},
|
|
|
|
"publish message unable to authorize": {
|
|
|
|
chanID: chanID,
|
|
|
|
msg: msg,
|
2022-06-13 12:04:48 +02:00
|
|
|
contentType: ctSenmlJSON,
|
2022-02-14 22:49:23 +01:00
|
|
|
key: mocks.ServiceErrToken,
|
|
|
|
status: http.StatusInternalServerError,
|
2018-06-16 02:30:46 +02:00
|
|
|
},
|
2018-04-11 14:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
req := testRequest{
|
2018-05-15 17:13:09 +02:00
|
|
|
client: ts.Client(),
|
2018-04-11 14:29:04 +02:00
|
|
|
method: http.MethodPost,
|
2018-06-16 02:30:46 +02:00
|
|
|
url: fmt.Sprintf("%s/channels/%s/messages", ts.URL, tc.chanID),
|
2018-04-11 14:29:04 +02:00
|
|
|
contentType: tc.contentType,
|
2022-02-14 22:49:23 +01:00
|
|
|
token: tc.key,
|
2018-04-11 14:29:04 +02:00
|
|
|
body: strings.NewReader(tc.msg),
|
2021-07-22 15:47:55 +02:00
|
|
|
basicAuth: tc.basicAuth,
|
2018-04-11 14:29:04 +02:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|