github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/auth/converter_test.go (about)

     1  package auth_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/model"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/domain/auth"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestConverter_ToGraphQL(t *testing.T) {
    14  	emptyCertCommonName := ""
    15  	// GIVEN
    16  	testCases := []struct {
    17  		Name     string
    18  		Input    *model.Auth
    19  		Expected *graphql.Auth
    20  		Error    error
    21  	}{
    22  		{
    23  			Name:     "All properties given for basic auth",
    24  			Input:    fixDetailedAuthBasicCredentials(),
    25  			Expected: fixDetailedBasicCredentialsGQLAuth(),
    26  		},
    27  		{
    28  			Name:     "All properties given for certificate oauth auth",
    29  			Input:    fixDetailedAuthCertificateOAuthCredentials(),
    30  			Expected: fixDetailedCertificateOAuthCredentialsGQLAuth(),
    31  		},
    32  		{
    33  			Name:     "All properties given for oauth auth",
    34  			Input:    fixDetailedOAuthCredentials(),
    35  			Expected: fixDetailedOAuthCredentialsGQLAuth(),
    36  		},
    37  		{
    38  			Name:  "Empty",
    39  			Input: &model.Auth{},
    40  			Expected: &graphql.Auth{
    41  				CertCommonName: &emptyCertCommonName,
    42  			},
    43  		},
    44  		{
    45  			Name:     "Nil",
    46  			Input:    nil,
    47  			Expected: nil,
    48  		},
    49  	}
    50  
    51  	for _, testCase := range testCases {
    52  		t.Run(testCase.Name, func(t *testing.T) {
    53  			// WHEN
    54  			converter := auth.NewConverter()
    55  			res, err := converter.ToGraphQL(testCase.Input)
    56  
    57  			// then
    58  			assert.NoError(t, err)
    59  			assert.Equal(t, testCase.Expected, res)
    60  		})
    61  	}
    62  }
    63  
    64  func TestConverter_InputFromGraphQL(t *testing.T) {
    65  	// GIVEN
    66  	testCases := []struct {
    67  		Name     string
    68  		Input    *graphql.AuthInput
    69  		Expected *model.AuthInput
    70  	}{
    71  		{
    72  			Name:     "All properties given for basic oauth",
    73  			Input:    fixDetailedBasicCredentialGQLAuthInput(),
    74  			Expected: fixDetailedBasicCredentialAuthInput(),
    75  		},
    76  		{
    77  			Name:     "All properties given for certificate oauth auth",
    78  			Input:    fixDetailedCertificateOAuthGQLAuthInput(),
    79  			Expected: fixDetailedCertificateOAuthAuthInput(),
    80  		},
    81  		{
    82  			Name:     "All properties given - deprecated",
    83  			Input:    fixDetailedGQLAuthInputDeprecated(),
    84  			Expected: fixDetailedBasicCredentialAuthInput(),
    85  		},
    86  		{
    87  			Name:     "Empty",
    88  			Input:    &graphql.AuthInput{},
    89  			Expected: &model.AuthInput{},
    90  		},
    91  	}
    92  
    93  	for _, testCase := range testCases {
    94  		t.Run(testCase.Name, func(t *testing.T) {
    95  			// WHEN
    96  			converter := auth.NewConverter()
    97  			res, err := converter.InputFromGraphQL(testCase.Input)
    98  
    99  			// then
   100  			assert.NoError(t, err)
   101  			assert.Equal(t, testCase.Expected, res)
   102  		})
   103  	}
   104  }
   105  
   106  func TestConverter_ModelFromGraphQLInput(t *testing.T) {
   107  	// GIVEN
   108  	gqlAuthInputWithInvalidHeaders := fixDetailedBasicCredentialGQLAuthInput()
   109  	gqlAuthInputWithInvalidHeaders.AdditionalHeadersSerialized = &invalidAuthHeadersSerialized
   110  
   111  	gqlAuthInputWithInvalidQueryParams := fixDetailedBasicCredentialGQLAuthInput()
   112  	gqlAuthInputWithInvalidQueryParams.AdditionalQueryParamsSerialized = &invalidAuthParamsSerialized
   113  
   114  	testCases := []struct {
   115  		Name             string
   116  		Input            graphql.AuthInput
   117  		Expected         *model.Auth
   118  		ExpectedErrorMsg string
   119  	}{
   120  		{
   121  			Name:     "All properties given for basic auth",
   122  			Input:    *fixDetailedBasicCredentialGQLAuthInput(),
   123  			Expected: fixDetailedAuthBasicCredentials(),
   124  		},
   125  		{
   126  			Name:     "All properties given for certificate oauth auth",
   127  			Input:    *fixDetailedCertificateOAuthGQLAuthInput(),
   128  			Expected: fixDetailedAuthCertificateOAuthCredentials(),
   129  		},
   130  		{
   131  			Name:     "Empty",
   132  			Input:    graphql.AuthInput{},
   133  			Expected: &model.Auth{},
   134  		},
   135  		{
   136  			Name:             "Error when HTTP serialized headers are invalid",
   137  			Input:            *gqlAuthInputWithInvalidHeaders,
   138  			Expected:         nil,
   139  			ExpectedErrorMsg: "unable to unmarshal HTTP headers",
   140  		},
   141  		{
   142  			Name:             "Error when query params are invalid",
   143  			Input:            *gqlAuthInputWithInvalidQueryParams,
   144  			Expected:         nil,
   145  			ExpectedErrorMsg: "unable to unmarshal query parameters",
   146  		},
   147  	}
   148  
   149  	for _, testCase := range testCases {
   150  		t.Run(testCase.Name, func(t *testing.T) {
   151  			// WHEN
   152  			converter := auth.NewConverter()
   153  			res, err := converter.ModelFromGraphQLInput(testCase.Input)
   154  
   155  			// then
   156  			if testCase.ExpectedErrorMsg != "" {
   157  				assert.Error(t, err)
   158  				assert.Contains(t, err.Error(), testCase.ExpectedErrorMsg)
   159  				assert.Equal(t, testCase.Expected, res)
   160  			} else {
   161  				assert.NoError(t, err)
   162  				assert.Equal(t, testCase.Expected, res)
   163  			}
   164  		})
   165  	}
   166  }
   167  
   168  func TestConverter_ModelFromGraphQLTokenInput(t *testing.T) {
   169  	// GIVEN
   170  	testCases := []struct {
   171  		Name     string
   172  		Input    *graphql.OneTimeTokenInput
   173  		Expected *model.OneTimeToken
   174  	}{
   175  		{
   176  			Name:     "All properties given",
   177  			Input:    fixOneTimeTokenGQLInput(),
   178  			Expected: fixOneTimeTokenInput(),
   179  		},
   180  		{
   181  			Name:     "Empty",
   182  			Input:    &graphql.OneTimeTokenInput{},
   183  			Expected: &model.OneTimeToken{},
   184  		},
   185  		{
   186  			Name:     "Nil",
   187  			Input:    nil,
   188  			Expected: nil,
   189  		},
   190  	}
   191  
   192  	for _, testCase := range testCases {
   193  		t.Run(testCase.Name, func(t *testing.T) {
   194  			// WHEN
   195  			converter := auth.NewConverter()
   196  			res := converter.ModelFromGraphQLTokenInput(testCase.Input)
   197  
   198  			// then
   199  			assert.Equal(t, testCase.Expected, res)
   200  		})
   201  	}
   202  }