Skip to content

Commit

Permalink
MG-888 - Add bootstrap SDK tests (absmach#2261)
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru authored Jun 26, 2024
1 parent 50b374b commit 1893107
Show file tree
Hide file tree
Showing 8 changed files with 1,333 additions and 30 deletions.
59 changes: 59 additions & 0 deletions bootstrap/mocks/config_reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
44 changes: 34 additions & 10 deletions pkg/sdk/go/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,27 @@ 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})
if err != nil {
return errors.NewSDKError(err)
func (sdk mgSDK) Whitelist(thingID string, state int, token string) errors.SDKError {
if thingID == "" {
return errors.NewSDKError(apiutil.ErrMissingID)
}

if cfg.ThingID == "" {
return errors.NewSDKError(apiutil.ErrNotFoundParam)
data, err := json.Marshal(BootstrapConfig{State: state})
if err != nil {
return errors.NewSDKError(err)
}

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)

return sdkerr
}

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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -216,13 +231,19 @@ 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)
return err
}

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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 1893107

Please sign in to comment.