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

     1  package systemauth_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/model"
     7  
     8  	pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model"
     9  
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/kyma-incubator/compass/components/director/internal/domain/systemauth"
    13  	"github.com/kyma-incubator/compass/components/director/internal/repo"
    14  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    15  
    16  	"github.com/DATA-DOG/go-sqlmock"
    17  )
    18  
    19  var (
    20  	testTenant           = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
    21  	testExternalTenant   = "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"
    22  	testMarshalledSchema = "{\"Credential\":{\"Basic\":{\"Username\":\"foo\",\"Password\":\"bar\"},\"Oauth\":null,\"CertificateOAuth\":null},\"AccessStrategy\":null,\"AdditionalHeaders\":{\"test\":[\"foo\",\"bar\"]},\"AdditionalQueryParams\":{\"test\":[\"foo\",\"bar\"]},\"RequestAuth\":{\"Csrf\":{\"TokenEndpointURL\":\"foo.url\",\"Credential\":{\"Basic\":{\"Username\":\"boo\",\"Password\":\"far\"},\"Oauth\":null,\"CertificateOAuth\":null},\"AdditionalHeaders\":{\"test\":[\"foo\",\"bar\"]},\"AdditionalQueryParams\":{\"test\":[\"foo\",\"bar\"]}}},\"OneTimeToken\":null,\"CertCommonName\":\"\"}"
    23  	testErr              = errors.New("test error")
    24  )
    25  
    26  var testTableColumns = []string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}
    27  
    28  func fixGQLAppSystemAuth(id string, auth *graphql.Auth, refObjID string) graphql.SystemAuth {
    29  	authType := graphql.SystemAuthReferenceTypeApplication
    30  	return &graphql.AppSystemAuth{
    31  		ID:                id,
    32  		Auth:              auth,
    33  		Type:              &authType,
    34  		TenantID:          &testTenant,
    35  		ReferenceObjectID: &refObjID,
    36  	}
    37  }
    38  
    39  func fixGQLIntSysSystemAuth(id string, auth *graphql.Auth, refObjID string) graphql.SystemAuth {
    40  	authType := graphql.SystemAuthReferenceTypeIntegrationSystem
    41  	return &graphql.IntSysSystemAuth{
    42  		ID:                id,
    43  		Auth:              auth,
    44  		Type:              &authType,
    45  		TenantID:          nil,
    46  		ReferenceObjectID: &refObjID,
    47  	}
    48  }
    49  
    50  func fixGQLRuntimeSystemAuth(id string, auth *graphql.Auth, refObjID string) graphql.SystemAuth {
    51  	authType := graphql.SystemAuthReferenceTypeRuntime
    52  	return &graphql.RuntimeSystemAuth{
    53  		ID:                id,
    54  		Auth:              auth,
    55  		Type:              &authType,
    56  		TenantID:          &testTenant,
    57  		ReferenceObjectID: &refObjID,
    58  	}
    59  }
    60  
    61  func fixModelSystemAuth(id string, objectType pkgmodel.SystemAuthReferenceObjectType, objectID string, auth *model.Auth) *pkgmodel.SystemAuth {
    62  	systemAuth := pkgmodel.SystemAuth{
    63  		ID:    id,
    64  		Value: auth,
    65  	}
    66  
    67  	switch objectType {
    68  	case pkgmodel.ApplicationReference:
    69  		systemAuth.AppID = &objectID
    70  		systemAuth.TenantID = &testTenant
    71  	case pkgmodel.RuntimeReference:
    72  		systemAuth.RuntimeID = &objectID
    73  		systemAuth.TenantID = &testTenant
    74  	case pkgmodel.IntegrationSystemReference:
    75  		systemAuth.IntegrationSystemID = &objectID
    76  		systemAuth.TenantID = nil
    77  	}
    78  
    79  	return &systemAuth
    80  }
    81  
    82  func fixModelAuthInput() model.AuthInput {
    83  	return model.AuthInput{
    84  		Credential: &model.CredentialDataInput{
    85  			Basic: &model.BasicCredentialDataInput{
    86  				Username: "foo",
    87  				Password: "bar",
    88  			},
    89  		},
    90  		AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
    91  		AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
    92  		RequestAuth: &model.CredentialRequestAuthInput{
    93  			Csrf: &model.CSRFTokenCredentialRequestAuthInput{
    94  				TokenEndpointURL: "foo.url",
    95  				Credential: &model.CredentialDataInput{
    96  					Basic: &model.BasicCredentialDataInput{
    97  						Username: "boo",
    98  						Password: "far",
    99  					},
   100  				},
   101  				AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
   102  				AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
   103  			},
   104  		},
   105  	}
   106  }
   107  
   108  func fixGQLAuth() *graphql.Auth {
   109  	return &graphql.Auth{
   110  		Credential: &graphql.BasicCredentialData{
   111  			Username: "foo",
   112  			Password: "bar",
   113  		},
   114  		AdditionalHeaders:     graphql.HTTPHeaders{"test": {"foo", "bar"}},
   115  		AdditionalQueryParams: graphql.QueryParams{"test": {"foo", "bar"}},
   116  		RequestAuth: &graphql.CredentialRequestAuth{
   117  			Csrf: &graphql.CSRFTokenCredentialRequestAuth{
   118  				TokenEndpointURL: "foo.url",
   119  				Credential: &graphql.BasicCredentialData{
   120  					Username: "boo",
   121  					Password: "far",
   122  				},
   123  				AdditionalHeaders:     graphql.HTTPHeaders{"test": {"foo", "bar"}},
   124  				AdditionalQueryParams: graphql.QueryParams{"test": {"foo", "bar"}},
   125  			},
   126  		},
   127  	}
   128  }
   129  
   130  func fixModelAuth() *model.Auth {
   131  	return &model.Auth{
   132  		Credential: model.CredentialData{
   133  			Basic: &model.BasicCredentialData{
   134  				Username: "foo",
   135  				Password: "bar",
   136  			},
   137  		},
   138  		AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
   139  		AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
   140  		RequestAuth: &model.CredentialRequestAuth{
   141  			Csrf: &model.CSRFTokenCredentialRequestAuth{
   142  				TokenEndpointURL: "foo.url",
   143  				Credential: model.CredentialData{
   144  					Basic: &model.BasicCredentialData{
   145  						Username: "boo",
   146  						Password: "far",
   147  					},
   148  				},
   149  				AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
   150  				AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
   151  			},
   152  		},
   153  		CertCommonName: "",
   154  	}
   155  }
   156  
   157  func fixGQLAuthInput() *graphql.AuthInput {
   158  	return &graphql.AuthInput{
   159  		Credential: &graphql.CredentialDataInput{
   160  			Basic: &graphql.BasicCredentialDataInput{
   161  				Username: "foo",
   162  				Password: "bar",
   163  			},
   164  		},
   165  		AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
   166  		AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
   167  		RequestAuth: &graphql.CredentialRequestAuthInput{
   168  			Csrf: &graphql.CSRFTokenCredentialRequestAuthInput{
   169  				TokenEndpointURL: "foo.url",
   170  				Credential: &graphql.CredentialDataInput{
   171  					Basic: &graphql.BasicCredentialDataInput{
   172  						Username: "foo",
   173  						Password: "bar",
   174  					},
   175  				},
   176  				AdditionalHeaders:     map[string][]string{"test": {"foo", "bar"}},
   177  				AdditionalQueryParams: map[string][]string{"test": {"foo", "bar"}},
   178  			},
   179  		},
   180  		CertCommonName: nil,
   181  	}
   182  }
   183  
   184  func fixEntity(id string, objectType pkgmodel.SystemAuthReferenceObjectType, objectID string, withAuth bool) systemauth.Entity {
   185  	out := systemauth.Entity{
   186  		ID: id,
   187  	}
   188  
   189  	switch objectType {
   190  	case pkgmodel.ApplicationReference:
   191  		out.AppID = repo.NewNullableString(&objectID)
   192  		out.TenantID = repo.NewNullableString(&testTenant)
   193  	case pkgmodel.RuntimeReference:
   194  		out.RuntimeID = repo.NewNullableString(&objectID)
   195  		out.TenantID = repo.NewNullableString(&testTenant)
   196  	case pkgmodel.IntegrationSystemReference:
   197  		out.IntegrationSystemID = repo.NewNullableString(&objectID)
   198  		out.TenantID = repo.NewNullableString(nil)
   199  	}
   200  
   201  	if withAuth {
   202  		out.Value = repo.NewNullableString(&testMarshalledSchema)
   203  	}
   204  
   205  	return out
   206  }
   207  
   208  type sqlRow struct {
   209  	id       string
   210  	tenant   *string
   211  	appID    *string
   212  	rtmID    *string
   213  	intSysID *string
   214  }
   215  
   216  func fixSQLRows(rows []sqlRow) *sqlmock.Rows {
   217  	out := sqlmock.NewRows(testTableColumns)
   218  	for _, row := range rows {
   219  		out.AddRow(row.id, row.tenant, row.appID, row.rtmID, row.intSysID, testMarshalledSchema)
   220  	}
   221  	return out
   222  }
   223  
   224  func fixSystemAuthCreateArgs(ent systemauth.Entity) []driver.Value {
   225  	return []driver.Value{ent.ID, ent.TenantID, ent.AppID, ent.RuntimeID, ent.IntegrationSystemID, ent.Value}
   226  }