1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Mainflux.mainflux/bootstrap/api/endpoint_test.go
Dušan Borovčanin 22fc26b375 MF-513 - Add Bootstrapping service (#524)
* Introduce Config response for bootstrap procedure

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add inital service implementation

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Enable status change

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix logger import

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update BSS to send config in valid format

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use ConfigReader to create valid format response

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update config retrieval error handle

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Enable Thing deletion API

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add API support for fetching Thing by ID

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add list Things endpoint

Update database schema

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use MF API to update status

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use Channels list

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix reading Thing from the database

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Create Mainflux Thing when adding new Thing to BS

Create MF Thing as soon as Bootstrap service thing is added. There are 2
main reasons to create Thing when adding a new BS Thing over creating
Thing on bootstrapping:
1) On bootstrapping time, user JWT will not be sent as a part of
request, so there is no mechanism to send a valid API call to Mainflux.
2) This way, Bootstrap service will be in sync with Mainlux: each Thing
existing in BS will also be in Mainflux.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add Thing update

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove API key from BS service

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Improve channels update algorithm

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Refactor code

Remove unused fields, comment code and simplfy some method signatures.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove Identity Provider and use gRPC

Update dependencies

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add external auth key

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update BS config reader

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update docker-compose

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update env variable read

Add MQTT password to bootstrap response.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update response fields and tags

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove status check

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Enable BS of active Things

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add NewThing state

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Rename Status to State

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update README.md

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add filterng

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update List endpoint

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix Database query

Remove copyright headers.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add filter type

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Gateway provisioning (1.d)

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update self-bootstrapping feature

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add mocks

Update dependencies to the newest Mainflux version.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add thing service tests

Mocks fix.
Some of the service code intentionally left untested due to possible
changes in future.
Fix copyright headers and update Mainflux and other dependencies.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use name "Config" instead of "Thing"

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Refactor code

Remove commented code.
Fix typo.
Remove unused exported error.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Simplify service tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove Assign method

Raise test coverage.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update database schema

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Store unknown bootstrap attempts

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update unknown bootstrap handling

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update naming

Fix uses of `Thing` in DB and `api` package.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add endpoint tests

Currently, only test for adding a new Config are implemented.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add initialization of DB tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add DB tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update readme file

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add API docs

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove Mainflux from vendor

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add licence headers

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix service and endpoint tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Raise test coverage

Remove unused repsonse type.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update build and deployment

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update API docs

Fix typo.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update imports formatting

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Make state response empty

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Raise test coverage

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update API docs

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update readme file

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use uuid as a primary key

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use Mainflux ID

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Remove `Created` state.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Move State to separate file

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add Things prefix

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update API and API docs

Be consistent in API naming and add some useful comments.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Refactor repository implementation

Cleanup code, make it more readable. Fix missing drop in migrations.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use "cfg" insted of "thing"

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update tables names

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
2019-01-09 15:42:23 +01:00

984 lines
26 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package api_test
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/bootstrap"
bsapi "github.com/mainflux/mainflux/bootstrap/api"
"github.com/mainflux/mainflux/bootstrap/mocks"
mfsdk "github.com/mainflux/mainflux/sdk/go"
"github.com/mainflux/mainflux/things"
thingsapi "github.com/mainflux/mainflux/things/api/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
validToken = "validToken"
invalidToken = "invalidToken"
email = "test@example.com"
unknown = "unknown"
channelsNum = 3
contentType = "application/json"
wrongID = "wrong_id"
)
type config struct {
MFThing string `json:"mainflux_id,omitempty"`
Owner string `json:"owner,omitempty"`
MFKey string `json:"mainflux_key,omitempty"`
MFChannels []string `json:"channels,omitempty"`
ExternalID string `json:"external_id,omitempty"`
ExternalKey string `json:"external_key,omitempty"`
Content string `json:"content,omitempty"`
State bootstrap.State `json:"state,omitempty"`
}
var cfg = config{
ExternalID: "external-id",
ExternalKey: "external-key",
MFChannels: []string{"1"},
Content: "config",
}
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 newService(users mainflux.UsersServiceClient, unknown map[string]string, url string) bootstrap.Service {
things := mocks.NewConfigsRepository(unknown)
config := mfsdk.Config{
BaseURL: url,
}
sdk := mfsdk.NewSDK(config)
return bootstrap.New(users, things, sdk)
}
func newThingsService(users mainflux.UsersServiceClient) things.Service {
channels := make(map[string]things.Channel, channelsNum)
for i := 0; i < channelsNum; i++ {
id := strconv.Itoa(i + 1)
channels[id] = things.Channel{
ID: id,
Owner: email,
}
}
return mocks.NewThingsService(map[string]things.Thing{}, channels, users)
}
func newThingsServer(svc things.Service) *httptest.Server {
mux := thingsapi.MakeHandler(svc)
return httptest.NewServer(mux)
}
func newBootstrapServer(svc bootstrap.Service) *httptest.Server {
mux := bsapi.MakeHandler(svc, bootstrap.NewConfigReader())
return httptest.NewServer(mux)
}
func toJSON(data interface{}) string {
jsonData, _ := json.Marshal(data)
return string(jsonData)
}
func TestAdd(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
data := toJSON(cfg)
invalidChannels := cfg
invalidChannels.MFChannels = []string{wrongID}
wrongData := toJSON(invalidChannels)
cases := []struct {
desc string
req string
auth string
contentType string
status int
location string
}{
{
desc: "add a config unauthorized",
req: data,
auth: invalidToken,
contentType: contentType,
status: http.StatusForbidden,
location: "",
},
{
desc: "add a valid config",
req: data,
auth: validToken,
contentType: contentType,
status: http.StatusCreated,
location: "/configs/1",
},
{
desc: "add a config with wring content type",
req: data,
auth: validToken,
contentType: "",
status: http.StatusUnsupportedMediaType,
location: "",
},
{
desc: "add an existing config",
req: data,
auth: validToken,
contentType: contentType,
status: http.StatusConflict,
location: "",
},
{
desc: "add a config with invalid channels",
req: wrongData,
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
location: "",
},
{
desc: "add a config with wrong JSON",
req: "{\"external_id\": 5}",
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
},
{
desc: "add a config with invalid request format",
req: "}",
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
location: "",
},
{
desc: "add a config with empty JSON",
req: "{}",
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
location: "",
},
{
desc: "add a config with an empty request",
req: "",
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
location: "",
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodPost,
url: fmt.Sprintf("%s/configs", bs.URL),
contentType: tc.contentType,
token: tc.auth,
body: strings.NewReader(tc.req),
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
location := res.Header.Get("Location")
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
assert.Equal(t, tc.location, location, fmt.Sprintf("%s: expected location '%s' got '%s'", tc.desc, tc.location, location))
}
}
func TestView(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
s := cfg
s.MFThing = saved.MFThing
s.MFThing = saved.MFThing
s.MFKey = saved.MFKey
s.State = saved.State
data := toJSON(s)
cases := []struct {
desc string
auth string
id string
status int
res string
}{
{
desc: "view a config unauthorized",
auth: invalidToken,
id: saved.MFThing,
status: http.StatusForbidden,
res: "",
},
{
desc: "view a config",
auth: validToken,
id: saved.MFThing,
status: http.StatusOK,
res: data,
},
{
desc: "view a non-existing config",
auth: validToken,
id: wrongID,
status: http.StatusNotFound,
res: "",
},
{
desc: "view a config with an empty token",
auth: "",
id: saved.MFThing,
status: http.StatusForbidden,
res: "",
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodGet,
url: fmt.Sprintf("%s/configs/%s", bs.URL, tc.id),
token: tc.auth,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
data := strings.Trim(string(body), "\n")
assert.Equal(t, tc.res, data, fmt.Sprintf("%s: expected response '%s' got '%s'", tc.desc, tc.res, data))
}
}
func TestUpdate(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
update := config{
MFChannels: []string{"2", "3"},
Content: "new config",
State: bootstrap.Active,
}
data := toJSON(update)
invalidChannels := update
invalidChannels.MFChannels = []string{wrongID}
wrongData := toJSON(invalidChannels)
cases := []struct {
desc string
req string
id string
auth string
contentType string
status int
}{
{
desc: "update unauthorized",
req: data,
id: saved.MFThing,
auth: invalidToken,
contentType: contentType,
status: http.StatusForbidden,
},
{
desc: "update with an empty token",
req: data,
id: saved.MFThing,
auth: "",
contentType: contentType,
status: http.StatusForbidden,
},
{
desc: "update a valid config",
req: data,
id: saved.MFThing,
auth: validToken,
contentType: contentType,
status: http.StatusOK,
},
{
desc: "update a config with wrong content type",
req: data,
id: saved.MFThing,
auth: validToken,
contentType: "",
status: http.StatusUnsupportedMediaType,
},
{
desc: "update a non-existing config",
req: data,
id: wrongID,
auth: validToken,
contentType: contentType,
status: http.StatusNotFound,
},
{
desc: "update a config with invalid channels",
req: wrongData,
id: saved.MFThing,
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
},
{
desc: "update a config with invalid request format",
req: "}",
id: saved.MFThing,
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
},
{
desc: "update a config with empty JSON",
req: "{}",
id: saved.MFThing,
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
},
{
desc: "update a config with an empty request",
id: saved.MFThing,
req: "",
auth: validToken,
contentType: contentType,
status: http.StatusBadRequest,
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodPut,
url: fmt.Sprintf("%s/configs/%s", bs.URL, tc.id),
contentType: tc.contentType,
token: tc.auth,
body: strings.NewReader(tc.req),
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
}
}
func TestList(t *testing.T) {
configNum := 101
changedStateNum := 20
var active, inactive []config
list := make([]config, configNum)
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
for i := 0; i < configNum; i++ {
c.ExternalID = strconv.Itoa(i)
c.MFKey = c.ExternalID
c.ExternalKey = fmt.Sprintf("%s%s", c.ExternalKey, strconv.Itoa(i))
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
s := config{
MFThing: saved.MFThing,
MFKey: saved.MFKey,
MFChannels: saved.MFChannels,
ExternalID: saved.ExternalID,
ExternalKey: saved.ExternalKey,
Content: saved.Content,
State: saved.State,
}
list[i] = s
}
// Change state of first 20 elements for filtering tests.
for i := 0; i < changedStateNum; i++ {
state := bootstrap.Active
if i%2 == 0 {
state = bootstrap.Inactive
}
err := svc.ChangeState(validToken, list[i].MFThing, state)
require.Nil(t, err, fmt.Sprintf("Changing state expected to succeed: %s.\n", err))
list[i].State = state
if state == bootstrap.Inactive {
inactive = append(inactive, list[i])
continue
}
active = append(active, list[i])
}
cases := []struct {
desc string
auth string
url string
status int
res []config
}{
{
desc: "view list unauthorized",
auth: invalidToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d", bs.URL, 0, 10),
status: http.StatusForbidden,
res: nil,
},
{
desc: "view list with an empty token",
auth: "",
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d", bs.URL, 0, 10),
status: http.StatusForbidden,
res: nil,
},
{
desc: "view list",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d", bs.URL, 0, 1),
status: http.StatusOK,
res: list[0:1],
},
{
desc: "view last page",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d", bs.URL, 100, 10),
status: http.StatusOK,
res: list[100:],
},
{
desc: "view with limit greater than allowed",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d", bs.URL, 0, 1000),
status: http.StatusOK,
res: list[:100],
},
{
desc: "view list with no specified limit and offset",
auth: validToken,
url: fmt.Sprintf("%s/configs", bs.URL),
status: http.StatusOK,
res: list[0:10],
},
{
desc: "view list with no specified limit",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d", bs.URL, 10),
status: http.StatusOK,
res: list[10:20],
},
{
desc: "view list with no specified offset",
auth: validToken,
url: fmt.Sprintf("%s/configs?limit=%d", bs.URL, 10),
status: http.StatusOK,
res: list[0:10],
},
{
desc: "view list with limit < 0",
auth: validToken,
url: fmt.Sprintf("%s/configs?limit=%d", bs.URL, -10),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view list with offset < 0",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d", bs.URL, -10),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view list with invalid query params",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d&state=%d&key=%%", bs.URL, 10, 10, bootstrap.Inactive),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view first 10 active",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d&state=%d", bs.URL, 0, 20, bootstrap.Active),
status: http.StatusOK,
res: active,
},
{
desc: "view first 10 inactive",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d&state=%d", bs.URL, 0, 20, bootstrap.Inactive),
status: http.StatusOK,
res: inactive,
},
{
desc: "view first 5 active",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d&state=%d", bs.URL, 0, 10, bootstrap.Active),
status: http.StatusOK,
res: active[:5],
},
{
desc: "view last 5 inactive",
auth: validToken,
url: fmt.Sprintf("%s/configs?offset=%d&limit=%d&state=%d", bs.URL, 10, 10, bootstrap.Inactive),
status: http.StatusOK,
res: inactive[5:],
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodGet,
url: tc.url,
token: tc.auth,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
var body map[string][]config
json.NewDecoder(res.Body).Decode(&body)
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.ElementsMatch(t, tc.res, body["configs"], fmt.Sprintf("%s: expected response '%s' got '%s'", tc.desc, tc.res, body["configs"]))
}
}
func TestRemove(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
cases := []struct {
desc string
id string
auth string
status int
}{
{
desc: "remove unauthorized",
id: saved.MFThing,
auth: invalidToken,
status: http.StatusForbidden,
}, {
desc: "remove with an empty token",
id: saved.MFThing,
auth: "",
status: http.StatusForbidden,
},
{
desc: "remove non-existing config",
id: "non-existing",
auth: validToken,
status: http.StatusNoContent,
},
{
desc: "remove config",
id: saved.MFThing,
auth: validToken,
status: http.StatusNoContent,
},
{
desc: "remove removed config",
id: wrongID,
auth: validToken,
status: http.StatusNoContent,
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodDelete,
url: fmt.Sprintf("%s/configs/%s", bs.URL, tc.id),
token: tc.auth,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
}
}
func TestListUnknown(t *testing.T) {
unknownNum := 10
unknown := make([]config, unknownNum)
unknownConfigs := make(map[string]string, unknownNum)
// Save some unknown elements.
for i := 0; i < unknownNum; i++ {
u := config{
ExternalID: fmt.Sprintf("key-%s", strconv.Itoa(i)),
ExternalKey: fmt.Sprintf("%s%s", cfg.ExternalKey, strconv.Itoa(i)),
}
unknownConfigs[u.ExternalID] = u.ExternalKey
unknown[i] = u
}
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, unknownConfigs, ts.URL)
bs := newBootstrapServer(svc)
cases := []struct {
desc string
auth string
url string
status int
res []config
}{
{
desc: "view unknown unauthorized",
auth: invalidToken,
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d", bs.URL, 0, 5),
status: http.StatusForbidden,
res: nil,
},
{
desc: "view unknown with an empty token",
auth: "",
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d", bs.URL, 0, 5),
status: http.StatusForbidden,
res: nil,
},
{
desc: "view unknown with limit < 0",
auth: validToken,
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d", bs.URL, 0, -5),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view unknown with offset < 0",
auth: validToken,
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d", bs.URL, -3, 5),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view unknown with invalid query params",
auth: validToken,
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d&key=%%", bs.URL, 0, -5),
status: http.StatusBadRequest,
res: nil,
},
{
desc: "view a list of unknown",
auth: validToken,
url: fmt.Sprintf("%s/unknown?offset=%d&limit=%d", bs.URL, 0, 5),
status: http.StatusOK,
res: unknown[:5],
},
{
desc: "view unknown with no page paremeters",
auth: validToken,
url: fmt.Sprintf("%s/unknown", bs.URL),
status: http.StatusOK,
res: unknown[:10],
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodGet,
url: tc.url,
token: tc.auth,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
var body map[string][]config
json.NewDecoder(res.Body).Decode(&body)
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.ElementsMatch(t, tc.res, body["configs"], fmt.Sprintf("%s: expected response '%s' got '%s'", tc.desc, tc.res, body["configs"]))
}
}
func TestBootstrap(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, map[string]string{}, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
s := config{
MFThing: saved.MFThing,
MFKey: saved.MFKey,
MFChannels: saved.MFChannels,
Content: saved.Content,
}
data, _ := json.Marshal(s)
// Bootstrapping response includes mainflux_channels instead of channels.
res := strings.Replace(string(data), "channels", "mainflux_channels", 1)
cases := []struct {
desc string
external_id string
external_key string
status int
res string
}{
{
desc: "bootstrap a Thing with unknown ID",
external_id: unknown,
external_key: c.ExternalKey,
status: http.StatusNotFound,
res: "",
},
{
desc: "bootstrap a Thing with an empty ID",
external_id: "",
external_key: c.ExternalKey,
status: http.StatusBadRequest,
res: "",
},
{
desc: "bootstrap a Thing with unknown key",
external_id: c.ExternalID,
external_key: unknown,
status: http.StatusNotFound,
res: "",
},
{
desc: "bootstrap a Thing with an empty key",
external_id: c.ExternalID,
external_key: "",
status: http.StatusForbidden,
res: "",
},
{
desc: "bootstrap known Thing",
external_id: c.ExternalID,
external_key: c.ExternalKey,
status: http.StatusOK,
res: res,
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodGet,
url: fmt.Sprintf("%s/bootstrap/%s", bs.URL, tc.external_id),
token: tc.external_key,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
data := strings.Trim(string(body), "\n")
assert.Equal(t, tc.res, data, fmt.Sprintf("%s: expected response '%s' got '%s'", tc.desc, tc.res, data))
}
}
func TestChangeState(t *testing.T) {
users := mocks.NewUsersService(map[string]string{validToken: email})
ts := newThingsServer(newThingsService(users))
svc := newService(users, nil, ts.URL)
bs := newBootstrapServer(svc)
c := bootstrap.Config{
ExternalID: cfg.ExternalID,
ExternalKey: cfg.ExternalKey,
MFChannels: cfg.MFChannels,
Content: cfg.Content,
}
saved, err := svc.Add(validToken, c)
require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
inactive := fmt.Sprintf("{\"state\": %d}", bootstrap.Inactive)
active := fmt.Sprintf("{\"state\": %d}", bootstrap.Active)
cases := []struct {
desc string
id string
auth string
state string
contentType string
status int
}{
{
desc: "change state unauthorized",
id: saved.MFThing,
auth: invalidToken,
state: active,
contentType: contentType,
status: http.StatusForbidden,
},
{
desc: "change state with an empty token",
id: saved.MFThing,
auth: "",
state: active,
contentType: contentType,
status: http.StatusForbidden,
},
{
desc: "change state with invalid content type",
id: saved.MFThing,
auth: validToken,
state: active,
contentType: "",
status: http.StatusUnsupportedMediaType,
},
{
desc: "change state to active",
id: saved.MFThing,
auth: validToken,
state: active,
contentType: contentType,
status: http.StatusOK,
},
{
desc: "change state to inactive",
id: saved.MFThing,
auth: validToken,
state: inactive,
contentType: contentType,
status: http.StatusOK,
},
{
desc: "change state of non-existing config",
id: wrongID,
auth: validToken,
state: active,
contentType: contentType,
status: http.StatusNotFound,
},
{
desc: "change state to invalid value",
id: saved.MFThing,
auth: validToken,
state: fmt.Sprintf("{\"state\": %d}", -3),
contentType: contentType,
status: http.StatusBadRequest,
},
{
desc: "change state with invalid data",
id: saved.MFThing,
auth: validToken,
state: "",
contentType: contentType,
status: http.StatusBadRequest,
},
}
for _, tc := range cases {
req := testRequest{
client: bs.Client(),
method: http.MethodPut,
url: fmt.Sprintf("%s/state/%s", bs.URL, tc.id),
token: tc.auth,
contentType: tc.contentType,
body: strings.NewReader(tc.state),
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
}
}