MG-888 - Add bootstrap SDK tests (#2261)

Signed-off-by: 1998-felix <felix.gateru@gmail.com>
This commit is contained in:
Felix Gateru
2024-06-26 15:45:05 +03:00
committed by GitHub
parent 50b374bd3e
commit 1893107b07
8 changed files with 1333 additions and 30 deletions
+59
View File
@@ -0,0 +1,59 @@
// Code generated by mockery v2.42.3. DO NOT EDIT.
// Copyright (c) Abstract Machines
package mocks
import (
bootstrap "github.com/absmach/magistrala/bootstrap"
mock "github.com/stretchr/testify/mock"
)
// ConfigReader is an autogenerated mock type for the ConfigReader type
type ConfigReader struct {
mock.Mock
}
// ReadConfig provides a mock function with given fields: _a0, _a1
func (_m *ConfigReader) ReadConfig(_a0 bootstrap.Config, _a1 bool) (interface{}, error) {
ret := _m.Called(_a0, _a1)
if len(ret) == 0 {
panic("no return value specified for ReadConfig")
}
var r0 interface{}
var r1 error
if rf, ok := ret.Get(0).(func(bootstrap.Config, bool) (interface{}, error)); ok {
return rf(_a0, _a1)
}
if rf, ok := ret.Get(0).(func(bootstrap.Config, bool) interface{}); ok {
r0 = rf(_a0, _a1)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(interface{})
}
}
if rf, ok := ret.Get(1).(func(bootstrap.Config, bool) error); ok {
r1 = rf(_a0, _a1)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// NewConfigReader creates a new instance of ConfigReader. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewConfigReader(t interface {
mock.TestingT
Cleanup(func())
}) *ConfigReader {
mock := &ConfigReader{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
+2
View File
@@ -108,6 +108,8 @@ type Service interface {
// as a JSON and consumed from the client side. The purpose of this interface
// is to provide convenient way to generate custom configuration response
// based on the specific Config which will be consumed by the client.
//
//go:generate mockery --name ConfigReader --output=./mocks --filename config_reader.go --quiet --note "Copyright (c) Abstract Machines"
type ConfigReader interface {
ReadConfig(Config, bool) (interface{}, error)
}
+1 -1
View File
@@ -190,7 +190,7 @@ var cmdBootstrap = []cobra.Command{
return
}
if err := sdk.Whitelist(cfg, args[1]); err != nil {
if err := sdk.Whitelist(cfg.ThingID, cfg.State, args[1]); err != nil {
logError(err)
return
}
+34 -10
View File
@@ -132,17 +132,17 @@ func (sdk mgSDK) Bootstraps(pm PageMetadata, token string) (BootstrapPage, error
return bb, nil
}
func (sdk mgSDK) Whitelist(cfg BootstrapConfig, token string) errors.SDKError {
data, err := json.Marshal(BootstrapConfig{State: cfg.State})
func (sdk mgSDK) Whitelist(thingID string, state int, token string) errors.SDKError {
if thingID == "" {
return errors.NewSDKError(apiutil.ErrMissingID)
}
data, err := json.Marshal(BootstrapConfig{State: state})
if err != nil {
return errors.NewSDKError(err)
}
if cfg.ThingID == "" {
return errors.NewSDKError(apiutil.ErrNotFoundParam)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, whitelistEndpoint, cfg.ThingID)
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, whitelistEndpoint, thingID)
_, _, sdkerr := sdk.processRequest(http.MethodPut, url, token, data, nil, http.StatusCreated, http.StatusOK)
@@ -150,6 +150,9 @@ func (sdk mgSDK) Whitelist(cfg BootstrapConfig, token string) errors.SDKError {
}
func (sdk mgSDK) ViewBootstrap(id, token string) (BootstrapConfig, errors.SDKError) {
if id == "" {
return BootstrapConfig{}, errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
_, body, err := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
@@ -166,19 +169,25 @@ func (sdk mgSDK) ViewBootstrap(id, token string) (BootstrapConfig, errors.SDKErr
}
func (sdk mgSDK) UpdateBootstrap(cfg BootstrapConfig, token string) errors.SDKError {
if cfg.ThingID == "" {
return errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, cfg.ThingID)
data, err := json.Marshal(cfg)
if err != nil {
return errors.NewSDKError(err)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, cfg.ThingID)
_, _, sdkerr := sdk.processRequest(http.MethodPut, url, token, data, nil, http.StatusOK)
return sdkerr
}
func (sdk mgSDK) UpdateBootstrapCerts(id, clientCert, clientKey, ca, token string) (BootstrapConfig, errors.SDKError) {
if id == "" {
return BootstrapConfig{}, errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapCertsEndpoint, id)
request := BootstrapConfig{
ClientCert: clientCert,
@@ -192,16 +201,22 @@ func (sdk mgSDK) UpdateBootstrapCerts(id, clientCert, clientKey, ca, token strin
}
_, body, sdkerr := sdk.processRequest(http.MethodPatch, url, token, data, nil, http.StatusOK)
if sdkerr != nil {
return BootstrapConfig{}, sdkerr
}
var bc BootstrapConfig
if err := json.Unmarshal(body, &bc); err != nil {
return BootstrapConfig{}, errors.NewSDKError(err)
}
return bc, sdkerr
return bc, nil
}
func (sdk mgSDK) UpdateBootstrapConnection(id string, channels []string, token string) errors.SDKError {
if id == "" {
return errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapConnEndpoint, id)
request := map[string][]string{
"channels": channels,
@@ -216,6 +231,9 @@ func (sdk mgSDK) UpdateBootstrapConnection(id string, channels []string, token s
}
func (sdk mgSDK) RemoveBootstrap(id, token string) errors.SDKError {
if id == "" {
return errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
_, _, err := sdk.processRequest(http.MethodDelete, url, token, nil, nil, http.StatusNoContent)
@@ -223,6 +241,9 @@ func (sdk mgSDK) RemoveBootstrap(id, token string) errors.SDKError {
}
func (sdk mgSDK) Bootstrap(externalID, externalKey string) (BootstrapConfig, errors.SDKError) {
if externalID == "" {
return BootstrapConfig{}, errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapEndpoint, externalID)
_, body, err := sdk.processRequest(http.MethodGet, url, ThingPrefix+externalKey, nil, nil, http.StatusOK)
@@ -239,6 +260,9 @@ func (sdk mgSDK) Bootstrap(externalID, externalKey string) (BootstrapConfig, err
}
func (sdk mgSDK) BootstrapSecure(externalID, externalKey, cryptoKey string) (BootstrapConfig, errors.SDKError) {
if externalID == "" {
return BootstrapConfig{}, errors.NewSDKError(apiutil.ErrMissingID)
}
url := fmt.Sprintf("%s/%s/%s/%s", sdk.bootstrapURL, bootstrapEndpoint, secureEndpoint, externalID)
encExtKey, err := bootstrapEncrypt([]byte(externalKey), cryptoKey)
File diff suppressed because it is too large Load Diff
+2 -9
View File
@@ -931,16 +931,9 @@ type SDK interface {
// Whitelist updates Thing state Config with given ID belonging to the user identified by the given token.
//
// example:
// cfg := sdk.BootstrapConfig{
// ThingID: "thingID",
// Name: "bootstrap",
// ExternalID: "externalID",
// ExternalKey: "externalKey",
// Channels: []string{"channel1", "channel2"},
// }
// err := sdk.Whitelist(cfg, "token")
// err := sdk.Whitelist("thingID", 1, "token")
// fmt.Println(err)
Whitelist(cfg BootstrapConfig, token string) errors.SDKError
Whitelist(thingID string, state int, token string) errors.SDKError
// IssueCert issues a certificate for a thing required for mTLS.
//
+5 -5
View File
@@ -2830,17 +2830,17 @@ func (_m *SDK) ViewSubscription(id string, token string) (sdk.Subscription, erro
return r0, r1
}
// Whitelist provides a mock function with given fields: cfg, token
func (_m *SDK) Whitelist(cfg sdk.BootstrapConfig, token string) errors.SDKError {
ret := _m.Called(cfg, token)
// Whitelist provides a mock function with given fields: thingID, state, token
func (_m *SDK) Whitelist(thingID string, state int, token string) errors.SDKError {
ret := _m.Called(thingID, state, token)
if len(ret) == 0 {
panic("no return value specified for Whitelist")
}
var r0 errors.SDKError
if rf, ok := ret.Get(0).(func(sdk.BootstrapConfig, string) errors.SDKError); ok {
r0 = rf(cfg, token)
if rf, ok := ret.Get(0).(func(string, int, string) errors.SDKError); ok {
r0 = rf(thingID, state, token)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(errors.SDKError)
+1 -5
View File
@@ -237,11 +237,7 @@ func (ps *provisionService) Provision(token, name, externalID, externalKey strin
}
if ps.conf.Bootstrap.AutoWhiteList {
wlReq := sdk.BootstrapConfig{
ThingID: thing.ID,
State: Active,
}
if err := ps.sdk.Whitelist(wlReq, token); err != nil {
if err := ps.sdk.Whitelist(thing.ID, Active, token); err != nil {
res.Error = err.Error()
return res, ErrThingUpdate
}