1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/bootstrap/service_test.go

832 lines
24 KiB
Go
Raw Normal View History

// Copyright (c) Mainflux
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
// SPDX-License-Identifier: Apache-2.0
package bootstrap_test
NOISSUE - Switch to Google Zanzibar Access control approach (#1919) * Return Auth service Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Compose to run with SpiceDB and Auth svc Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth gRPC API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Users' policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Groups to internal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use shared groups in Users Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove unused code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use pkg Groups in Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Things groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Make imports consistent Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Groups networking Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove things groups-specific API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Things Clients to the root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Clients to Users root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Temporarily remove tracing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix imports Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add buffer config for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth type for Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use Auth for login Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add temporary solution for refresh token Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Tokenizer interface Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Updade tokens issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix token issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update JWT validator and refactor Tokenizer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename access timeout Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename login to authenticate Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Identify to use SubjectID Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add Auth to Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use the Auth service for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth schema Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix Auth for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add auth for addons (#14) Signed-off-by: Arvindh <arvindh91@gmail.com> Speparate Login and Refresh tokens Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Merge authN and authZ requests for things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add connect and disconnect Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update sharing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix policies addition and removal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update relation with roels Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add gRPC to Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Assign and Unassign members to group and Listing of Group members (#15) * add auth for addons Signed-off-by: Arvindh <arvindh91@gmail.com> * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Move coap mqtt and ws policies to spicedb (#16) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Remove old policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> NOISSUE - Things authorize to return thingID (#18) This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Add Groups to users service (#17) * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 1 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 2 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users & things Signed-off-by: Arvindh <arvindh91@gmail.com> * Amend signature Signed-off-by: Arvindh <arvindh91@gmail.com> * fix merge error Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix es code (#21) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix Bugs (#20) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Test e2e (#19) * fix: connect method Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix: e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix changes in sdk and e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(docker): remove unnecessary port mapping Remove the port mapping for MQTT broker in the docker-compose.yml file. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Enable group listing Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(responses): update ChannelsPage struct The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(things): add UpdateClientSecret method Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use smaller buffers for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean up tests (#22) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add Connect Disconnect endpoints (#23) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Things share with users (#25) * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * add: things share with other users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Rename gRPC Services (#24) * Rename things and users auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * docs: add authorization docs for gRPC services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Rename things and users grpc services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove mainflux.env package Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Listing of things, channels, groups, users (#26) * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Users (#27) * feat(groups): rename redis package to events - Renamed the `redis` package to `events` in the `internal/groups` directory. - Updated the file paths and names accordingly. - This change reflects the more accurate purpose of the package and improves code organization. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): Modify identity method Change request and response of identity method Add accessToken and refreshToken to Token response Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * clean up users, remove dead code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(users): add unit tests for user service This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh <arvindh91@gmail.com> * add: list of user group Signed-off-by: Arvindh <arvindh91@gmail.com> * fix: otel handler operator name for endpoints Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Things Service (#28) * Rework things service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * add tests Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Auth Service (#30) * clean up auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): remove unused import Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update API docs (#31) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TODO comments and cleanup the code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependenices Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
2023-10-15 22:02:13 +02:00
// import (
// "context"
// "crypto/aes"
// "crypto/cipher"
// "crypto/rand"
// "encoding/hex"
// "fmt"
// "io"
// "net/http/httptest"
// "sort"
// "strconv"
// "testing"
// "github.com/go-zoo/bone"
// "github.com/gofrs/uuid"
// "github.com/mainflux/mainflux/bootstrap"
// "github.com/mainflux/mainflux/bootstrap/mocks"
// mflog "github.com/mainflux/mainflux/logger"
// mfclients "github.com/mainflux/mainflux/pkg/clients"
// "github.com/mainflux/mainflux/pkg/errors"
// mfgroups "github.com/mainflux/mainflux/pkg/groups"
// mfsdk "github.com/mainflux/mainflux/pkg/sdk/go"
// "github.com/mainflux/mainflux/things/clients"
// capi "github.com/mainflux/mainflux/things/clients/api"
// "github.com/mainflux/mainflux/things/groups"
// gapi "github.com/mainflux/mainflux/things/groups/api"
// tpolicies "github.com/mainflux/mainflux/things/policies"
// papi "github.com/mainflux/mainflux/things/policies/api/http"
// upolicies "github.com/mainflux/mainflux/users/policies"
// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/require"
// )
// const (
// validToken = "validToken"
// invalidToken = "invalidToken"
// email = "test@example.com"
// unknown = "unknown"
// channelsNum = 3
// instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002"
// )
// var (
// encKey = []byte("1234567891011121")
// channel = bootstrap.Channel{
// ID: "1",
// Name: "name",
// Metadata: map[string]interface{}{"name": "value"},
// }
// config = bootstrap.Config{
// ExternalID: "external_id",
// ExternalKey: "external_key",
// Channels: []bootstrap.Channel{channel},
// Content: "config",
// }
// )
// func newService(auth upolicies.AuthServiceClient, url string) bootstrap.Service {
// things := mocks.NewConfigsRepository()
// config := mfsdk.Config{
// ThingsURL: url,
// }
// sdk := mfsdk.NewSDK(config)
// return bootstrap.New(auth, things, sdk, encKey)
// }
// func newThingsService(auth upolicies.AuthServiceClient) (clients.Service, groups.Service, tpolicies.Service) {
// channels := make(map[string]mfgroups.Group, channelsNum)
// for i := 0; i < channelsNum; i++ {
// id := strconv.Itoa(i + 1)
// channels[id] = mfgroups.Group{
// ID: id,
// Owner: email,
// Metadata: map[string]interface{}{"meta": "data"},
// Status: mfclients.EnabledStatus,
// }
// }
// csvc := mocks.NewThingsService(map[string]mfclients.Client{}, auth)
// gsvc := mocks.NewChannelsService(channels, auth)
// psvc := mocks.NewPoliciesService(auth)
// return csvc, gsvc, psvc
// }
// func newThingsServer(csvc clients.Service, gsvc groups.Service, psvc tpolicies.Service) *httptest.Server {
// logger := mflog.NewMock()
// mux := bone.New()
// capi.MakeHandler(csvc, mux, logger, instanceID)
// gapi.MakeHandler(gsvc, mux, logger)
// papi.MakeHandler(csvc, psvc, mux, logger)
// return httptest.NewServer(mux)
// }
// func enc(in []byte) ([]byte, error) {
// block, err := aes.NewCipher(encKey)
// if err != nil {
// return nil, err
// }
// ciphertext := make([]byte, aes.BlockSize+len(in))
// iv := ciphertext[:aes.BlockSize]
// if _, err := io.ReadFull(rand.Reader, iv); err != nil {
// return nil, err
// }
// stream := cipher.NewCFBEncrypter(block, iv)
// stream.XORKeyStream(ciphertext[aes.BlockSize:], in)
// return ciphertext, nil
// }
// func TestAdd(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// neID := config
// neID.ThingID = "non-existent"
// wrongChannels := config
// ch := channel
// ch.ID = "invalid"
// wrongChannels.Channels = append(wrongChannels.Channels, ch)
// cases := []struct {
// desc string
// config bootstrap.Config
// token string
// err error
// }{
// {
// desc: "add a new config",
// config: config,
// token: validToken,
// err: nil,
// },
// {
// desc: "add a config with an invalid ID",
// config: neID,
// token: validToken,
// err: errors.ErrNotFound,
// },
// {
// desc: "add a config with wrong credentials",
// config: config,
// token: invalidToken,
// err: errors.ErrAuthentication,
// },
// {
// desc: "add a config with invalid list of channels",
// config: wrongChannels,
// token: validToken,
// err: errors.ErrMalformedEntity,
// },
// }
// for _, tc := range cases {
// _, err := svc.Add(context.Background(), tc.token, tc.config)
// switch err {
// case nil:
// assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error : %s", tc.desc, err))
// default:
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// }
// func TestView(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// id string
// token string
// err error
// }{
// {
// desc: "view an existing config",
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// {
// desc: "view a non-existing config",
// id: unknown,
// token: validToken,
// err: errors.ErrNotFound,
// },
// {
// desc: "view a config with wrong credentials",
// id: config.ThingID,
// token: invalidToken,
// err: errors.ErrAuthentication,
// },
// }
// for _, tc := range cases {
// _, err := svc.View(context.Background(), tc.token, tc.id)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestUpdate(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// c := config
// ch := channel
// ch.ID = "2"
// c.Channels = append(c.Channels, ch)
// saved, err := svc.Add(context.Background(), validToken, c)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// modifiedCreated := saved
// modifiedCreated.Content = "new-config"
// modifiedCreated.Name = "new name"
// nonExisting := config
// nonExisting.ThingID = unknown
// cases := []struct {
// desc string
// config bootstrap.Config
// token string
// err error
// }{
// {
// desc: "update a config with state Created",
// config: modifiedCreated,
// token: validToken,
// err: nil,
// },
// {
// desc: "update a non-existing config",
// config: nonExisting,
// token: validToken,
// err: errors.ErrNotFound,
// },
// {
// desc: "update a config with wrong credentials",
// config: saved,
// token: invalidToken,
// err: errors.ErrAuthentication,
// },
// }
// for _, tc := range cases {
// err := svc.Update(context.Background(), tc.token, tc.config)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestUpdateCert(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// c := config
// ch := channel
// ch.ID = "2"
// c.Channels = append(c.Channels, ch)
// saved, err := svc.Add(context.Background(), validToken, c)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// token string
// thingKey string
// clientCert string
// clientKey string
// caCert string
// expectedConfig bootstrap.Config
// err error
// }{
// {
// desc: "update certs for the valid config",
// thingKey: saved.ThingKey,
// clientCert: "newCert",
// clientKey: "newKey",
// caCert: "newCert",
// token: validToken,
// expectedConfig: bootstrap.Config{
// Name: saved.Name,
// ThingKey: saved.ThingKey,
// Channels: saved.Channels,
// ExternalID: saved.ExternalID,
// ExternalKey: saved.ExternalKey,
// Content: saved.Content,
// State: saved.State,
// Owner: saved.Owner,
// ThingID: saved.ThingID,
// ClientCert: "newCert",
// CACert: "newCert",
// ClientKey: "newKey",
// },
// err: nil,
// },
// {
// desc: "update cert for a non-existing config",
// thingKey: "empty",
// clientCert: "newCert",
// clientKey: "newKey",
// caCert: "newCert",
// token: validToken,
// expectedConfig: bootstrap.Config{},
// err: errors.ErrNotFound,
// },
// {
// desc: "update config cert with wrong credentials",
// thingKey: saved.ThingKey,
// clientCert: "newCert",
// clientKey: "newKey",
// caCert: "newCert",
// token: invalidToken,
// expectedConfig: bootstrap.Config{},
// err: errors.ErrAuthentication,
// },
// }
// for _, tc := range cases {
// cfg, err := svc.UpdateCert(context.Background(), tc.token, tc.thingKey, tc.clientCert, tc.clientKey, tc.caCert)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// sort.Slice(cfg.Channels, func(i, j int) bool {
// return cfg.Channels[i].ID < cfg.Channels[j].ID
// })
// sort.Slice(tc.expectedConfig.Channels, func(i, j int) bool {
// return tc.expectedConfig.Channels[i].ID < tc.expectedConfig.Channels[j].ID
// })
// assert.Equal(t, tc.expectedConfig, cfg, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.expectedConfig, cfg))
// }
// }
// func TestUpdateConnections(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// c := config
// ch := channel
// ch.ID = "2"
// c.Channels = append(c.Channels, ch)
// created, err := svc.Add(context.Background(), validToken, c)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// externalID, err := uuid.NewV4()
// assert.Nil(t, err, fmt.Sprintf("Got unexpected error: %s.\n", err))
// c.ExternalID = externalID.String()
// active, err := svc.Add(context.Background(), validToken, c)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// err = svc.ChangeState(context.Background(), validToken, active.ThingID, bootstrap.Active)
// assert.Nil(t, err, fmt.Sprintf("Changing state expected to succeed: %s.\n", err))
// nonExisting := config
// nonExisting.ThingID = unknown
// cases := []struct {
// desc string
// token string
// id string
// connections []string
// err error
// }{
// {
// desc: "update connections for config with state Inactive",
// token: validToken,
// id: created.ThingID,
// connections: []string{"2"},
// err: nil,
// },
// {
// desc: "update connections for config with state Active",
// token: validToken,
// id: active.ThingID,
// connections: []string{"3"},
// err: nil,
// },
// {
// desc: "update connections for non-existing config",
// token: validToken,
// id: "",
// connections: []string{"3"},
// err: errors.ErrNotFound,
// },
// {
// desc: "update connections with invalid channels",
// token: validToken,
// id: created.ThingID,
// connections: []string{"wrong"},
// err: errors.ErrMalformedEntity,
// },
// {
// desc: "update connections a config with wrong credentials",
// token: invalidToken,
// id: created.ThingKey,
// connections: []string{"2", "3"},
// err: errors.ErrAuthentication,
// },
// }
// for _, tc := range cases {
// err := svc.UpdateConnections(context.Background(), tc.token, tc.id, tc.connections)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestList(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// numThings := 101
// var saved []bootstrap.Config
// for i := 0; i < numThings; i++ {
// c := config
// id, err := uuid.NewV4()
// assert.Nil(t, err, fmt.Sprintf("Got unexpected error: %s.\n", err))
// c.ExternalID = id.String()
// c.ExternalKey = id.String()
// c.Name = fmt.Sprintf("%s-%d", config.Name, i)
// s, err := svc.Add(context.Background(), validToken, c)
// saved = append(saved, s)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// }
// // Set one Thing to the different state
// err := svc.ChangeState(context.Background(), validToken, "42", bootstrap.Active)
// assert.Nil(t, err, fmt.Sprintf("Changing config state expected to succeed: %s.\n", err))
// saved[41].State = bootstrap.Active
// cases := []struct {
// desc string
// config bootstrap.ConfigsPage
// filter bootstrap.Filter
// offset uint64
// limit uint64
// token string
// err error
// }{
// {
// desc: "list configs",
// config: bootstrap.ConfigsPage{
// Total: uint64(len(saved)),
// Offset: 0,
// Limit: 10,
// Configs: saved[0:10],
// },
// filter: bootstrap.Filter{},
// token: validToken,
// offset: 0,
// limit: 10,
// err: nil,
// },
// {
// desc: "list configs with specified name",
// config: bootstrap.ConfigsPage{
// Total: 1,
// Offset: 0,
// Limit: 100,
// Configs: saved[95:96],
// },
// filter: bootstrap.Filter{PartialMatch: map[string]string{"name": "95"}},
// token: validToken,
// offset: 0,
// limit: 100,
// err: nil,
// },
// {
// desc: "list configs with invalid token",
// config: bootstrap.ConfigsPage{},
// filter: bootstrap.Filter{},
// token: invalidToken,
// offset: 0,
// limit: 10,
// err: errors.ErrAuthentication,
// },
// {
// desc: "list last page",
// config: bootstrap.ConfigsPage{
// Total: uint64(len(saved)),
// Offset: 95,
// Limit: 10,
// Configs: saved[95:],
// },
// filter: bootstrap.Filter{},
// token: validToken,
// offset: 95,
// limit: 10,
// err: nil,
// },
// {
// desc: "list configs with Active state",
// config: bootstrap.ConfigsPage{
// Total: 1,
// Offset: 35,
// Limit: 20,
// Configs: []bootstrap.Config{saved[41]},
// },
// filter: bootstrap.Filter{FullMatch: map[string]string{"state": bootstrap.Active.String()}},
// token: validToken,
// offset: 35,
// limit: 20,
// err: nil,
// },
// }
// for _, tc := range cases {
// result, err := svc.List(context.Background(), tc.token, tc.filter, tc.offset, tc.limit)
// assert.ElementsMatch(t, tc.config.Configs, result.Configs, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.config.Configs, result.Configs))
// assert.Equal(t, tc.config.Total, result.Total, fmt.Sprintf("%s: expected %v got %v", tc.desc, tc.config.Total, result.Total))
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestRemove(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// id string
// token string
// err error
// }{
// {
// desc: "view a config with wrong credentials",
// id: saved.ThingID,
// token: invalidToken,
// err: errors.ErrAuthentication,
// },
// {
// desc: "remove an existing config",
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// {
// desc: "remove removed config",
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// {
// desc: "remove non-existing config",
// id: unknown,
// token: validToken,
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.Remove(context.Background(), tc.token, tc.id)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestBootstrap(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// e, err := enc([]byte(saved.ExternalKey))
// assert.Nil(t, err, fmt.Sprintf("Encrypting external key expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// config bootstrap.Config
// externalKey string
// externalID string
// err error
// encrypted bool
// }{
// {
// desc: "bootstrap using invalid external id",
// config: bootstrap.Config{},
// externalID: "invalid",
// externalKey: saved.ExternalKey,
// err: errors.ErrNotFound,
// encrypted: false,
// },
// {
// desc: "bootstrap using invalid external key",
// config: bootstrap.Config{},
// externalID: saved.ExternalID,
// externalKey: "invalid",
// err: bootstrap.ErrExternalKey,
// encrypted: false,
// },
// {
// desc: "bootstrap an existing config",
// config: saved,
// externalID: saved.ExternalID,
// externalKey: saved.ExternalKey,
// err: nil,
// encrypted: false,
// },
// {
// desc: "bootstrap encrypted",
// config: saved,
// externalID: saved.ExternalID,
// externalKey: hex.EncodeToString(e),
// err: nil,
// encrypted: true,
// },
// }
// for _, tc := range cases {
// config, err := svc.Bootstrap(context.Background(), tc.externalKey, tc.externalID, tc.encrypted)
// assert.Equal(t, tc.config, config, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.config, config))
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestChangeState(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// state bootstrap.State
// id string
// token string
// err error
// }{
// {
// desc: "change state with wrong credentials",
// state: bootstrap.Active,
// id: saved.ThingID,
// token: invalidToken,
// err: errors.ErrAuthentication,
// },
// {
// desc: "change state of non-existing config",
// state: bootstrap.Active,
// id: unknown,
// token: validToken,
// err: errors.ErrNotFound,
// },
// {
// desc: "change state to Active",
// state: bootstrap.Active,
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// {
// desc: "change state to current state",
// state: bootstrap.Active,
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// {
// desc: "change state to Inactive",
// state: bootstrap.Inactive,
// id: saved.ThingID,
// token: validToken,
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.ChangeState(context.Background(), tc.token, tc.id, tc.state)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestUpdateChannelHandler(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// _, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// ch := bootstrap.Channel{
// ID: channel.ID,
// Name: "new name",
// Metadata: map[string]interface{}{"meta": "new"},
// }
// cases := []struct {
// desc string
// channel bootstrap.Channel
// err error
// }{
// {
// desc: "update an existing channel",
// channel: ch,
// err: nil,
// },
// {
// desc: "update a non-existing channel",
// channel: bootstrap.Channel{ID: ""},
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.UpdateChannelHandler(context.Background(), tc.channel)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestRemoveChannelHandler(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// _, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// id string
// err error
// }{
// {
// desc: "remove an existing channel",
// id: channel.ID,
// err: nil,
// },
// {
// desc: "remove a non-existing channel",
// id: "unknown",
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.RemoveChannelHandler(context.Background(), tc.id)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestRemoveCoinfigHandler(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// assert.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// id string
// err error
// }{
// {
// desc: "remove an existing config",
// id: saved.ThingID,
// err: nil,
// },
// {
// desc: "remove a non-existing channel",
// id: "unknown",
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.RemoveConfigHandler(context.Background(), tc.id)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }
// func TestDisconnectThingsHandler(t *testing.T) {
// users := mocks.NewAuthClient(map[string]string{validToken: email})
// server := newThingsServer(newThingsService(users))
// svc := newService(users, server.URL)
// saved, err := svc.Add(context.Background(), validToken, config)
// require.Nil(t, err, fmt.Sprintf("Saving config expected to succeed: %s.\n", err))
// cases := []struct {
// desc string
// thingID string
// channelID string
// err error
// }{
// {
// desc: "disconnect",
// channelID: channel.ID,
// thingID: saved.ThingID,
// err: nil,
// },
// {
// desc: "disconnect disconnected",
// channelID: channel.ID,
// thingID: saved.ThingID,
// err: nil,
// },
// }
// for _, tc := range cases {
// err := svc.DisconnectThingHandler(context.Background(), tc.channelID, tc.thingID)
// assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
// }
// }