dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/common/secret_test.go (about)

     1  //
     2  // Copyright (c) 2020 Intel Corporation
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package common
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    27  )
    28  
    29  const (
    30  	TestUUID = "82eb2e26-0f24-48aa-ae4c-de9dac3fb9bc"
    31  )
    32  
    33  var validRequest = SecretRequest{
    34  	BaseRequest: BaseRequest{
    35  		RequestId:   TestUUID,
    36  		Versionable: NewVersionable(),
    37  	},
    38  	SecretName: "something",
    39  	SecretData: []SecretDataKeyValue{
    40  		{Key: "username", Value: "User1"},
    41  		{Key: "password", Value: "password"},
    42  	},
    43  }
    44  
    45  var missingKeySecretData = []SecretDataKeyValue{
    46  	{Key: "", Value: "password"},
    47  }
    48  
    49  var missingValueSecretData = []SecretDataKeyValue{
    50  	{Key: "password", Value: ""},
    51  }
    52  
    53  func TestSecretsRequest_Validate(t *testing.T) {
    54  	validNoPath := validRequest
    55  	validNoPath.SecretName = ""
    56  	validWithPath := validRequest
    57  	validNoRequestId := validRequest
    58  	validNoRequestId.RequestId = ""
    59  	badRequestId := validRequest
    60  	badRequestId.RequestId = "Bad Request Id"
    61  	noSecrets := validRequest
    62  	noSecrets.SecretData = []SecretDataKeyValue{}
    63  	missingSecretKey := validRequest
    64  	missingSecretKey.SecretData = missingKeySecretData
    65  	missingSecretValue := validRequest
    66  	missingSecretValue.SecretData = missingValueSecretData
    67  
    68  	tests := []struct {
    69  		Name          string
    70  		Request       SecretRequest
    71  		ErrorExpected bool
    72  	}{
    73  		{"valid - with with path", validWithPath, false},
    74  		{"valid - no requestId", validNoRequestId, false},
    75  		{"invalid - with no path", validNoPath, true},
    76  		{"invalid - bad requestId", badRequestId, true},
    77  		{"invalid - no Secrets", noSecrets, true},
    78  		{"invalid - missing secret key", missingSecretKey, true},
    79  		{"invalid - missing secret value", missingSecretValue, true},
    80  	}
    81  	for _, testCase := range tests {
    82  		t.Run(testCase.Name, func(t *testing.T) {
    83  			err := testCase.Request.Validate()
    84  			if testCase.ErrorExpected {
    85  				require.Error(t, err)
    86  				return // Test complete
    87  			}
    88  
    89  			require.NoError(t, err)
    90  		})
    91  	}
    92  }
    93  
    94  func TestSecretsRequest_UnmarshalJSON(t *testing.T) {
    95  	resultTestBytes, _ := json.Marshal(validRequest)
    96  
    97  	tests := []struct {
    98  		Name          string
    99  		Expected      SecretRequest
   100  		Data          []byte
   101  		ErrorExpected bool
   102  		ErrorKind     errors.ErrKind
   103  	}{
   104  		{"unmarshal with success", validRequest, resultTestBytes, false, ""},
   105  		{"unmarshal invalid, empty data", SecretRequest{}, []byte{}, true, errors.KindContractInvalid},
   106  		{"unmarshal invalid, non-json data", SecretRequest{}, []byte("Invalid SecretRequest"), true, errors.KindContractInvalid},
   107  	}
   108  
   109  	for _, testCase := range tests {
   110  		t.Run(testCase.Name, func(t *testing.T) {
   111  			actual := SecretRequest{}
   112  			err := actual.UnmarshalJSON(testCase.Data)
   113  			if testCase.ErrorExpected {
   114  				require.Error(t, err)
   115  				require.Equal(t, testCase.ErrorKind, errors.Kind(err))
   116  				return // Test complete
   117  			}
   118  
   119  			require.NoError(t, err)
   120  			assert.Equal(t, testCase.Expected, actual, "Unmarshal did not result in expected SecretRequest.")
   121  		})
   122  	}
   123  }