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

     1  package systemauth_test
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/model"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/internal/domain/systemauth/automock"
    14  
    15  	"github.com/DATA-DOG/go-sqlmock"
    16  	"github.com/kyma-incubator/compass/components/director/internal/domain/systemauth"
    17  	"github.com/kyma-incubator/compass/components/director/internal/repo/testdb"
    18  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestRepository_Create(t *testing.T) {
    23  	// GIVEN
    24  	sysAuthID := "foo"
    25  	objID := "bar"
    26  
    27  	modelAuth := fixModelAuth()
    28  
    29  	insertQuery := `^INSERT INTO public.system_auths \(.+\) VALUES \(.+\)$`
    30  
    31  	t.Run("Success creating auth for Runtime", func(t *testing.T) {
    32  		db, dbMock := testdb.MockDatabase(t)
    33  		ctx := persistence.SaveToContext(context.TODO(), db)
    34  
    35  		modelSysAuth := fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth)
    36  		entSysAuth := fixEntity(sysAuthID, model.RuntimeReference, objID, true)
    37  
    38  		dbMock.ExpectExec(insertQuery).
    39  			WithArgs(fixSystemAuthCreateArgs(entSysAuth)...).
    40  			WillReturnResult(sqlmock.NewResult(-1, 1))
    41  
    42  		convMock := automock.Converter{}
    43  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
    44  		pgRepository := systemauth.NewRepository(&convMock)
    45  
    46  		// WHEN
    47  		err := pgRepository.Create(ctx, *modelSysAuth)
    48  
    49  		// THEN
    50  		require.NoError(t, err)
    51  		dbMock.AssertExpectations(t)
    52  		convMock.AssertExpectations(t)
    53  	})
    54  
    55  	t.Run("Success creating auth for Application", func(t *testing.T) {
    56  		db, dbMock := testdb.MockDatabase(t)
    57  		ctx := persistence.SaveToContext(context.TODO(), db)
    58  
    59  		modelSysAuth := fixModelSystemAuth("foo", model.ApplicationReference, objID, modelAuth)
    60  		entSysAuth := fixEntity(sysAuthID, model.ApplicationReference, objID, true)
    61  
    62  		dbMock.ExpectExec(insertQuery).
    63  			WithArgs(fixSystemAuthCreateArgs(entSysAuth)...).
    64  			WillReturnResult(sqlmock.NewResult(-1, 1))
    65  
    66  		convMock := automock.Converter{}
    67  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
    68  		pgRepository := systemauth.NewRepository(&convMock)
    69  
    70  		// WHEN
    71  		err := pgRepository.Create(ctx, *modelSysAuth)
    72  
    73  		// THEN
    74  		require.NoError(t, err)
    75  		dbMock.AssertExpectations(t)
    76  		convMock.AssertExpectations(t)
    77  	})
    78  
    79  	t.Run("Success creating auth for Integration System", func(t *testing.T) {
    80  		db, dbMock := testdb.MockDatabase(t)
    81  		ctx := persistence.SaveToContext(context.TODO(), db)
    82  
    83  		modelSysAuth := fixModelSystemAuth("foo", model.IntegrationSystemReference, objID, modelAuth)
    84  		entSysAuth := fixEntity(sysAuthID, model.IntegrationSystemReference, objID, true)
    85  
    86  		dbMock.ExpectExec(insertQuery).
    87  			WithArgs(fixSystemAuthCreateArgs(entSysAuth)...).
    88  			WillReturnResult(sqlmock.NewResult(-1, 1))
    89  
    90  		convMock := automock.Converter{}
    91  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
    92  		pgRepository := systemauth.NewRepository(&convMock)
    93  
    94  		// WHEN
    95  		err := pgRepository.Create(ctx, *modelSysAuth)
    96  
    97  		// THEN
    98  		require.NoError(t, err)
    99  		dbMock.AssertExpectations(t)
   100  		convMock.AssertExpectations(t)
   101  	})
   102  
   103  	t.Run("Error converting", func(t *testing.T) {
   104  		ctx := context.TODO()
   105  
   106  		modelSysAuth := fixModelSystemAuth("foo", model.IntegrationSystemReference, objID, modelAuth)
   107  
   108  		convMock := automock.Converter{}
   109  		convMock.On("ToEntity", *modelSysAuth).Return(systemauth.Entity{}, testErr).Once()
   110  		pgRepository := systemauth.NewRepository(&convMock)
   111  
   112  		// WHEN
   113  		err := pgRepository.Create(ctx, *modelSysAuth)
   114  
   115  		// THEN
   116  		require.Error(t, err)
   117  		assert.Contains(t, err.Error(), testErr.Error())
   118  		convMock.AssertExpectations(t)
   119  	})
   120  
   121  	t.Run("Error creating", func(t *testing.T) {
   122  		db, dbMock := testdb.MockDatabase(t)
   123  		ctx := persistence.SaveToContext(context.TODO(), db)
   124  
   125  		modelSysAuth := fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth)
   126  		entSysAuth := fixEntity(sysAuthID, model.RuntimeReference, objID, true)
   127  
   128  		dbMock.ExpectExec(insertQuery).
   129  			WithArgs(fixSystemAuthCreateArgs(entSysAuth)...).
   130  			WillReturnError(testErr)
   131  
   132  		convMock := automock.Converter{}
   133  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
   134  		pgRepository := systemauth.NewRepository(&convMock)
   135  
   136  		// WHEN
   137  		err := pgRepository.Create(ctx, *modelSysAuth)
   138  
   139  		// THEN
   140  		require.Error(t, err)
   141  		assert.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   142  
   143  		dbMock.AssertExpectations(t)
   144  		convMock.AssertExpectations(t)
   145  	})
   146  }
   147  
   148  func TestRepository_GetByIDForObject(t *testing.T) {
   149  	saID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
   150  	objectID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
   151  
   152  	t.Run("Success for Application", func(t *testing.T) {
   153  		// GIVEN
   154  		saModel := fixModelSystemAuth(saID, model.ApplicationReference, objectID, fixModelAuth())
   155  		saEntity := fixEntity(saID, model.ApplicationReference, objectID, true)
   156  
   157  		mockConverter := &automock.Converter{}
   158  		mockConverter.On("FromEntity", saEntity).Return(*saModel, nil).Once()
   159  		defer mockConverter.AssertExpectations(t)
   160  
   161  		repo := systemauth.NewRepository(mockConverter)
   162  		db, dbMock := testdb.MockDatabase(t)
   163  		defer dbMock.AssertExpectations(t)
   164  
   165  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   166  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   167  
   168  		query := "SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE tenant_id = $1 AND id = $2 AND app_id IS NOT NULL"
   169  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   170  			WithArgs(testTenant, saID).WillReturnRows(rows)
   171  
   172  		ctx := persistence.SaveToContext(context.TODO(), db)
   173  		// WHEN
   174  		actual, err := repo.GetByIDForObject(ctx, testTenant, saID, model.ApplicationReference)
   175  		// THEN
   176  		require.NoError(t, err)
   177  		require.NotNil(t, actual)
   178  		assert.Equal(t, saModel, actual)
   179  	})
   180  
   181  	t.Run("Success for Runtime", func(t *testing.T) {
   182  		// GIVEN
   183  		saModel := fixModelSystemAuth(saID, model.RuntimeReference, objectID, fixModelAuth())
   184  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   185  
   186  		mockConverter := &automock.Converter{}
   187  		mockConverter.On("FromEntity", saEntity).Return(*saModel, nil).Once()
   188  		defer mockConverter.AssertExpectations(t)
   189  
   190  		repo := systemauth.NewRepository(mockConverter)
   191  		db, dbMock := testdb.MockDatabase(t)
   192  		defer dbMock.AssertExpectations(t)
   193  
   194  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   195  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   196  
   197  		query := "SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE tenant_id = $1 AND id = $2 AND runtime_id IS NOT NULL"
   198  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   199  			WithArgs(testTenant, saID).WillReturnRows(rows)
   200  
   201  		ctx := persistence.SaveToContext(context.TODO(), db)
   202  		// WHEN
   203  		actual, err := repo.GetByIDForObject(ctx, testTenant, saID, model.RuntimeReference)
   204  		// THEN
   205  		require.NoError(t, err)
   206  		require.NotNil(t, actual)
   207  		assert.Equal(t, saModel, actual)
   208  	})
   209  
   210  	t.Run("Error - Converter", func(t *testing.T) {
   211  		// GIVEN
   212  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   213  
   214  		mockConverter := &automock.Converter{}
   215  		defer mockConverter.AssertExpectations(t)
   216  		mockConverter.On("FromEntity", saEntity).Return(model.SystemAuth{}, givenError())
   217  
   218  		repo := systemauth.NewRepository(mockConverter)
   219  		db, dbMock := testdb.MockDatabase(t)
   220  		defer dbMock.AssertExpectations(t)
   221  
   222  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   223  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   224  
   225  		dbMock.ExpectQuery("SELECT .*").
   226  			WithArgs(testTenant, saID).WillReturnRows(rows)
   227  
   228  		ctx := persistence.SaveToContext(context.TODO(), db)
   229  		// WHEN
   230  		_, err := repo.GetByIDForObject(ctx, testTenant, saID, model.RuntimeReference)
   231  		// THEN
   232  		require.EqualError(t, err, "while converting SystemAuth entity to model: some error")
   233  	})
   234  
   235  	t.Run("Error - DB", func(t *testing.T) {
   236  		// GIVEN
   237  		repo := systemauth.NewRepository(nil)
   238  		db, dbMock := testdb.MockDatabase(t)
   239  		defer dbMock.AssertExpectations(t)
   240  
   241  		dbMock.ExpectQuery("SELECT .*").
   242  			WithArgs(testTenant, saID).WillReturnError(givenError())
   243  
   244  		ctx := persistence.SaveToContext(context.TODO(), db)
   245  		// WHEN
   246  		_, err := repo.GetByIDForObject(ctx, testTenant, saID, model.RuntimeReference)
   247  		// THEN
   248  		require.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   249  	})
   250  }
   251  
   252  func TestRepository_GetByIDForObjectGlobal(t *testing.T) {
   253  	saID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
   254  	objectID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
   255  
   256  	t.Run("Success", func(t *testing.T) {
   257  		// GIVEN
   258  		saModel := fixModelSystemAuth(saID, model.RuntimeReference, objectID, fixModelAuth())
   259  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   260  
   261  		mockConverter := &automock.Converter{}
   262  		mockConverter.On("FromEntity", saEntity).Return(*saModel, nil).Once()
   263  		defer mockConverter.AssertExpectations(t)
   264  
   265  		repo := systemauth.NewRepository(mockConverter)
   266  		db, dbMock := testdb.MockDatabase(t)
   267  		defer dbMock.AssertExpectations(t)
   268  
   269  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   270  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   271  
   272  		query := "SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE id = $1 AND runtime_id IS NOT NULL"
   273  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   274  			WithArgs(saID).WillReturnRows(rows)
   275  
   276  		ctx := persistence.SaveToContext(context.TODO(), db)
   277  		// WHEN
   278  		actual, err := repo.GetByIDForObjectGlobal(ctx, saID, model.RuntimeReference)
   279  		// THEN
   280  		require.NoError(t, err)
   281  		require.NotNil(t, actual)
   282  		assert.Equal(t, saModel, actual)
   283  	})
   284  
   285  	t.Run("Error - Converter", func(t *testing.T) {
   286  		// GIVEN
   287  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   288  
   289  		mockConverter := &automock.Converter{}
   290  		defer mockConverter.AssertExpectations(t)
   291  		mockConverter.On("FromEntity", saEntity).Return(model.SystemAuth{}, givenError())
   292  
   293  		repo := systemauth.NewRepository(mockConverter)
   294  		db, dbMock := testdb.MockDatabase(t)
   295  		defer dbMock.AssertExpectations(t)
   296  
   297  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   298  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   299  
   300  		dbMock.ExpectQuery("SELECT .*").
   301  			WithArgs(saID).WillReturnRows(rows)
   302  
   303  		ctx := persistence.SaveToContext(context.TODO(), db)
   304  		// WHEN
   305  		_, err := repo.GetByIDForObjectGlobal(ctx, saID, model.RuntimeReference)
   306  		// THEN
   307  		require.EqualError(t, err, "while converting SystemAuth entity to model: some error")
   308  	})
   309  
   310  	t.Run("Error - DB", func(t *testing.T) {
   311  		// GIVEN
   312  		repo := systemauth.NewRepository(nil)
   313  		db, dbMock := testdb.MockDatabase(t)
   314  		defer dbMock.AssertExpectations(t)
   315  
   316  		dbMock.ExpectQuery("SELECT .*").
   317  			WithArgs(saID).WillReturnError(givenError())
   318  
   319  		ctx := persistence.SaveToContext(context.TODO(), db)
   320  		// WHEN
   321  		_, err := repo.GetByIDForObjectGlobal(ctx, saID, model.RuntimeReference)
   322  		// THEN
   323  		require.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   324  	})
   325  }
   326  
   327  func TestRepository_GetByIDGlobal(t *testing.T) {
   328  	saID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
   329  	objectID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
   330  
   331  	t.Run("Success", func(t *testing.T) {
   332  		// GIVEN
   333  		saModel := fixModelSystemAuth(saID, model.RuntimeReference, objectID, fixModelAuth())
   334  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   335  
   336  		mockConverter := &automock.Converter{}
   337  		mockConverter.On("FromEntity", saEntity).Return(*saModel, nil).Once()
   338  		defer mockConverter.AssertExpectations(t)
   339  
   340  		repo := systemauth.NewRepository(mockConverter)
   341  		db, dbMock := testdb.MockDatabase(t)
   342  		defer dbMock.AssertExpectations(t)
   343  
   344  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   345  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   346  
   347  		query := "SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE id = $1"
   348  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   349  			WithArgs(saID).WillReturnRows(rows)
   350  
   351  		ctx := persistence.SaveToContext(context.TODO(), db)
   352  		// WHEN
   353  		actual, err := repo.GetByIDGlobal(ctx, saID)
   354  		// THEN
   355  		require.NoError(t, err)
   356  		require.NotNil(t, actual)
   357  		assert.Equal(t, saModel, actual)
   358  	})
   359  
   360  	t.Run("Error - Converter", func(t *testing.T) {
   361  		// GIVEN
   362  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   363  
   364  		mockConverter := &automock.Converter{}
   365  		defer mockConverter.AssertExpectations(t)
   366  		mockConverter.On("FromEntity", saEntity).Return(model.SystemAuth{}, givenError())
   367  
   368  		repo := systemauth.NewRepository(mockConverter)
   369  		db, dbMock := testdb.MockDatabase(t)
   370  		defer dbMock.AssertExpectations(t)
   371  
   372  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   373  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   374  
   375  		dbMock.ExpectQuery("SELECT .*").
   376  			WithArgs(saID).WillReturnRows(rows)
   377  
   378  		ctx := persistence.SaveToContext(context.TODO(), db)
   379  		// WHEN
   380  		_, err := repo.GetByIDGlobal(ctx, saID)
   381  		// THEN
   382  		require.EqualError(t, err, "while converting SystemAuth entity to model: some error")
   383  	})
   384  
   385  	t.Run("Error - DB", func(t *testing.T) {
   386  		// GIVEN
   387  		repo := systemauth.NewRepository(nil)
   388  		db, dbMock := testdb.MockDatabase(t)
   389  		defer dbMock.AssertExpectations(t)
   390  
   391  		dbMock.ExpectQuery("SELECT .*").
   392  			WithArgs(saID).WillReturnError(givenError())
   393  
   394  		ctx := persistence.SaveToContext(context.TODO(), db)
   395  		// WHEN
   396  		_, err := repo.GetByIDGlobal(ctx, saID)
   397  		// THEN
   398  		require.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   399  	})
   400  }
   401  
   402  func TestRepository_ListForObject(t *testing.T) {
   403  	// GIVEN
   404  	objID := "bar"
   405  
   406  	modelAuth := fixModelAuth()
   407  
   408  	t.Run("Success listing auths for Runtime", func(t *testing.T) {
   409  		db, dbMock := testdb.MockDatabase(t)
   410  		ctx := persistence.SaveToContext(context.TODO(), db)
   411  
   412  		modelSysAuths := []*model.SystemAuth{
   413  			fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth),
   414  			fixModelSystemAuth("bar", model.RuntimeReference, objID, modelAuth),
   415  		}
   416  		entSysAuths := []systemauth.Entity{
   417  			fixEntity("foo", model.RuntimeReference, objID, true),
   418  			fixEntity("bar", model.RuntimeReference, objID, true),
   419  		}
   420  
   421  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE tenant_id = $1 AND runtime_id = $2`
   422  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   423  			WithArgs(testTenant, objID).
   424  			WillReturnRows(fixSQLRows([]sqlRow{
   425  				{
   426  					id:       modelSysAuths[0].ID,
   427  					tenant:   &testTenant,
   428  					appID:    modelSysAuths[0].AppID,
   429  					rtmID:    modelSysAuths[0].RuntimeID,
   430  					intSysID: modelSysAuths[0].IntegrationSystemID,
   431  				},
   432  				{
   433  					id:       modelSysAuths[1].ID,
   434  					tenant:   &testTenant,
   435  					appID:    modelSysAuths[1].AppID,
   436  					rtmID:    modelSysAuths[1].RuntimeID,
   437  					intSysID: modelSysAuths[1].IntegrationSystemID,
   438  				},
   439  			}))
   440  
   441  		convMock := automock.Converter{}
   442  		convMock.On("FromEntity", entSysAuths[0]).Return(*modelSysAuths[0], nil).Once()
   443  		convMock.On("FromEntity", entSysAuths[1]).Return(*modelSysAuths[1], nil).Once()
   444  		pgRepository := systemauth.NewRepository(&convMock)
   445  
   446  		// WHEN
   447  		result, err := pgRepository.ListForObject(ctx, testTenant, model.RuntimeReference, objID)
   448  
   449  		// THEN
   450  		require.NoError(t, err)
   451  		require.NotNil(t, result)
   452  		dbMock.AssertExpectations(t)
   453  		convMock.AssertExpectations(t)
   454  	})
   455  
   456  	t.Run("Success listing auths for Application", func(t *testing.T) {
   457  		db, dbMock := testdb.MockDatabase(t)
   458  		ctx := persistence.SaveToContext(context.TODO(), db)
   459  
   460  		modelSysAuths := []*model.SystemAuth{
   461  			fixModelSystemAuth("foo", model.ApplicationReference, objID, modelAuth),
   462  			fixModelSystemAuth("bar", model.ApplicationReference, objID, modelAuth),
   463  		}
   464  		entSysAuths := []systemauth.Entity{
   465  			fixEntity("foo", model.ApplicationReference, objID, true),
   466  			fixEntity("bar", model.ApplicationReference, objID, true),
   467  		}
   468  
   469  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE tenant_id = $1 AND app_id = $2`
   470  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   471  			WithArgs(testTenant, objID).
   472  			WillReturnRows(fixSQLRows([]sqlRow{
   473  				{
   474  					id:       modelSysAuths[0].ID,
   475  					tenant:   &testTenant,
   476  					appID:    modelSysAuths[0].AppID,
   477  					rtmID:    modelSysAuths[0].RuntimeID,
   478  					intSysID: modelSysAuths[0].IntegrationSystemID,
   479  				},
   480  				{
   481  					id:       modelSysAuths[1].ID,
   482  					tenant:   &testTenant,
   483  					appID:    modelSysAuths[1].AppID,
   484  					rtmID:    modelSysAuths[1].RuntimeID,
   485  					intSysID: modelSysAuths[1].IntegrationSystemID,
   486  				},
   487  			}))
   488  
   489  		convMock := automock.Converter{}
   490  		convMock.On("FromEntity", entSysAuths[0]).Return(*modelSysAuths[0], nil).Once()
   491  		convMock.On("FromEntity", entSysAuths[1]).Return(*modelSysAuths[1], nil).Once()
   492  		pgRepository := systemauth.NewRepository(&convMock)
   493  
   494  		// WHEN
   495  		result, err := pgRepository.ListForObject(ctx, testTenant, model.ApplicationReference, objID)
   496  
   497  		// THEN
   498  		require.NoError(t, err)
   499  		require.NotNil(t, result)
   500  		dbMock.AssertExpectations(t)
   501  		convMock.AssertExpectations(t)
   502  	})
   503  
   504  	t.Run("Success listing auths for Integration System", func(t *testing.T) {
   505  		db, dbMock := testdb.MockDatabase(t)
   506  		ctx := persistence.SaveToContext(context.TODO(), db)
   507  
   508  		modelSysAuths := []*model.SystemAuth{
   509  			fixModelSystemAuth("foo", model.IntegrationSystemReference, objID, modelAuth),
   510  			fixModelSystemAuth("bar", model.IntegrationSystemReference, objID, modelAuth),
   511  		}
   512  		entSysAuths := []systemauth.Entity{
   513  			fixEntity("foo", model.IntegrationSystemReference, objID, true),
   514  			fixEntity("bar", model.IntegrationSystemReference, objID, true),
   515  		}
   516  
   517  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE integration_system_id = $1`
   518  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   519  			WithArgs(objID).
   520  			WillReturnRows(fixSQLRows([]sqlRow{
   521  				{
   522  					id:       modelSysAuths[0].ID,
   523  					tenant:   nil,
   524  					appID:    modelSysAuths[0].AppID,
   525  					rtmID:    modelSysAuths[0].RuntimeID,
   526  					intSysID: modelSysAuths[0].IntegrationSystemID,
   527  				},
   528  				{
   529  					id:       modelSysAuths[1].ID,
   530  					tenant:   nil,
   531  					appID:    modelSysAuths[1].AppID,
   532  					rtmID:    modelSysAuths[1].RuntimeID,
   533  					intSysID: modelSysAuths[1].IntegrationSystemID,
   534  				},
   535  			}))
   536  
   537  		convMock := automock.Converter{}
   538  		convMock.On("FromEntity", entSysAuths[0]).Return(*modelSysAuths[0], nil).Once()
   539  		convMock.On("FromEntity", entSysAuths[1]).Return(*modelSysAuths[1], nil).Once()
   540  		pgRepository := systemauth.NewRepository(&convMock)
   541  
   542  		// WHEN
   543  		result, err := pgRepository.ListForObjectGlobal(ctx, model.IntegrationSystemReference, objID)
   544  
   545  		// THEN
   546  		require.NoError(t, err)
   547  		require.NotNil(t, result)
   548  		dbMock.AssertExpectations(t)
   549  		convMock.AssertExpectations(t)
   550  	})
   551  
   552  	t.Run("Error listing auths for unsupported reference object type", func(t *testing.T) {
   553  		pgRepository := systemauth.NewRepository(nil)
   554  		errorMsg := "unsupported reference object type"
   555  
   556  		// WHEN
   557  		result, err := pgRepository.ListForObject(context.TODO(), testTenant, "unsupported", objID)
   558  
   559  		// THEN
   560  		require.Error(t, err)
   561  		assert.Contains(t, err.Error(), errorMsg)
   562  		require.Nil(t, result)
   563  	})
   564  
   565  	t.Run("Error listing auths", func(t *testing.T) {
   566  		db, dbMock := testdb.MockDatabase(t)
   567  		ctx := persistence.SaveToContext(context.TODO(), db)
   568  
   569  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE integration_system_id = $1`
   570  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   571  			WithArgs(objID).
   572  			WillReturnError(testErr)
   573  
   574  		pgRepository := systemauth.NewRepository(nil)
   575  
   576  		// WHEN
   577  		result, err := pgRepository.ListForObjectGlobal(ctx, model.IntegrationSystemReference, objID)
   578  
   579  		// THEN
   580  		require.Error(t, err)
   581  		assert.Contains(t, err.Error(), "Internal Server Error: Unexpected error while executing SQL query")
   582  		require.Nil(t, result)
   583  		dbMock.AssertExpectations(t)
   584  	})
   585  
   586  	t.Run("Error converting auth", func(t *testing.T) {
   587  		db, dbMock := testdb.MockDatabase(t)
   588  		ctx := persistence.SaveToContext(context.TODO(), db)
   589  
   590  		modelSysAuths := []*model.SystemAuth{
   591  			fixModelSystemAuth("foo", model.IntegrationSystemReference, objID, modelAuth),
   592  			fixModelSystemAuth("bar", model.IntegrationSystemReference, objID, modelAuth),
   593  		}
   594  		entSysAuths := []systemauth.Entity{
   595  			fixEntity("foo", model.IntegrationSystemReference, objID, true),
   596  			fixEntity("bar", model.IntegrationSystemReference, objID, true),
   597  		}
   598  
   599  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE integration_system_id = $1`
   600  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   601  			WithArgs(objID).
   602  			WillReturnRows(fixSQLRows([]sqlRow{
   603  				{
   604  					id:       modelSysAuths[0].ID,
   605  					tenant:   nil,
   606  					appID:    modelSysAuths[0].AppID,
   607  					rtmID:    modelSysAuths[0].RuntimeID,
   608  					intSysID: modelSysAuths[0].IntegrationSystemID,
   609  				},
   610  				{
   611  					id:       modelSysAuths[1].ID,
   612  					tenant:   nil,
   613  					appID:    modelSysAuths[1].AppID,
   614  					rtmID:    modelSysAuths[1].RuntimeID,
   615  					intSysID: modelSysAuths[1].IntegrationSystemID,
   616  				},
   617  			}))
   618  
   619  		convMock := automock.Converter{}
   620  		convMock.On("FromEntity", entSysAuths[0]).Return(model.SystemAuth{}, testErr).Once()
   621  		pgRepository := systemauth.NewRepository(&convMock)
   622  
   623  		// WHEN
   624  		result, err := pgRepository.ListForObjectGlobal(ctx, model.IntegrationSystemReference, objID)
   625  
   626  		// THEN
   627  		require.Error(t, err)
   628  		assert.Contains(t, err.Error(), testErr.Error())
   629  		require.Nil(t, result)
   630  		dbMock.AssertExpectations(t)
   631  		convMock.AssertExpectations(t)
   632  	})
   633  }
   634  
   635  func TestRepository_DeleteAllForObject(t *testing.T) {
   636  	// GIVEN
   637  	sysAuthID := "foo"
   638  
   639  	t.Run("Success deleting auth for Runtime", func(t *testing.T) {
   640  		db, dbMock := testdb.MockDatabase(t)
   641  		ctx := persistence.SaveToContext(context.TODO(), db)
   642  
   643  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND runtime_id = $2`
   644  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   645  			WithArgs(testTenant, sysAuthID).
   646  			WillReturnResult(sqlmock.NewResult(-1, 1))
   647  
   648  		repo := systemauth.NewRepository(nil)
   649  		// WHEN
   650  		err := repo.DeleteAllForObject(ctx, testTenant, model.RuntimeReference, sysAuthID)
   651  		// THEN
   652  		require.NoError(t, err)
   653  		dbMock.AssertExpectations(t)
   654  	})
   655  
   656  	t.Run("Success deleting auth for Application", func(t *testing.T) {
   657  		db, dbMock := testdb.MockDatabase(t)
   658  		ctx := persistence.SaveToContext(context.TODO(), db)
   659  
   660  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND app_id = $2`
   661  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   662  			WithArgs(testTenant, sysAuthID).
   663  			WillReturnResult(sqlmock.NewResult(-1, 1))
   664  
   665  		repo := systemauth.NewRepository(nil)
   666  		// WHEN
   667  		err := repo.DeleteAllForObject(ctx, testTenant, model.ApplicationReference, sysAuthID)
   668  		// THEN
   669  		require.NoError(t, err)
   670  		dbMock.AssertExpectations(t)
   671  	})
   672  
   673  	t.Run("Success deleting auth for Integration System", func(t *testing.T) {
   674  		db, dbMock := testdb.MockDatabase(t)
   675  		ctx := persistence.SaveToContext(context.TODO(), db)
   676  
   677  		query := `DELETE FROM public.system_auths WHERE integration_system_id = $1`
   678  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   679  			WithArgs(sysAuthID).
   680  			WillReturnResult(sqlmock.NewResult(-1, 1))
   681  
   682  		repo := systemauth.NewRepository(nil)
   683  		// WHEN
   684  		err := repo.DeleteAllForObject(ctx, "", model.IntegrationSystemReference, sysAuthID)
   685  		// THEN
   686  		require.NoError(t, err)
   687  		dbMock.AssertExpectations(t)
   688  	})
   689  
   690  	t.Run("Error when deleting", func(t *testing.T) {
   691  		db, dbMock := testdb.MockDatabase(t)
   692  		ctx := persistence.SaveToContext(context.TODO(), db)
   693  
   694  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND runtime_id = $2`
   695  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   696  			WithArgs(testTenant, sysAuthID).
   697  			WillReturnError(testErr)
   698  
   699  		repo := systemauth.NewRepository(nil)
   700  		// WHEN
   701  		err := repo.DeleteAllForObject(ctx, testTenant, model.RuntimeReference, sysAuthID)
   702  		// THEN
   703  		require.Error(t, err)
   704  		assert.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   705  		dbMock.AssertExpectations(t)
   706  	})
   707  
   708  	t.Run("Error listing auths for unsupported reference object type", func(t *testing.T) {
   709  		pgRepository := systemauth.NewRepository(nil)
   710  		errorMsg := "unsupported reference object type"
   711  
   712  		// WHEN
   713  		err := pgRepository.DeleteAllForObject(context.TODO(), testTenant, "unsupported", "foo")
   714  
   715  		// THEN
   716  		require.Error(t, err)
   717  		assert.Contains(t, err.Error(), errorMsg)
   718  	})
   719  }
   720  
   721  func TestRepository_DeleteByIDForObject(t *testing.T) {
   722  	// GIVEN
   723  	sysAuthID := "foo"
   724  
   725  	t.Run("Success when deleting by application", func(t *testing.T) {
   726  		db, dbMock := testdb.MockDatabase(t)
   727  		ctx := persistence.SaveToContext(context.TODO(), db)
   728  
   729  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND id = $2 AND app_id IS NOT NULL`
   730  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   731  			WithArgs(testTenant, sysAuthID).
   732  			WillReturnResult(sqlmock.NewResult(-1, 1))
   733  
   734  		repo := systemauth.NewRepository(nil)
   735  		// WHEN
   736  		err := repo.DeleteByIDForObject(ctx, testTenant, sysAuthID, model.ApplicationReference)
   737  		// THEN
   738  		require.NoError(t, err)
   739  		dbMock.AssertExpectations(t)
   740  	})
   741  
   742  	t.Run("Success when deleting by runtime", func(t *testing.T) {
   743  		db, dbMock := testdb.MockDatabase(t)
   744  		ctx := persistence.SaveToContext(context.TODO(), db)
   745  
   746  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND id = $2 AND runtime_id IS NOT NULL`
   747  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   748  			WithArgs(testTenant, sysAuthID).
   749  			WillReturnResult(sqlmock.NewResult(-1, 1))
   750  
   751  		repo := systemauth.NewRepository(nil)
   752  		// WHEN
   753  		err := repo.DeleteByIDForObject(ctx, testTenant, sysAuthID, model.RuntimeReference)
   754  		// THEN
   755  		require.NoError(t, err)
   756  		dbMock.AssertExpectations(t)
   757  	})
   758  
   759  	t.Run("Success when deleting by integration system", func(t *testing.T) {
   760  		db, dbMock := testdb.MockDatabase(t)
   761  		ctx := persistence.SaveToContext(context.TODO(), db)
   762  
   763  		query := `DELETE FROM public.system_auths WHERE id = $1 AND integration_system_id IS NOT NULL`
   764  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   765  			WithArgs(sysAuthID).
   766  			WillReturnResult(sqlmock.NewResult(-1, 1))
   767  
   768  		repo := systemauth.NewRepository(nil)
   769  		// WHEN
   770  		err := repo.DeleteByIDForObjectGlobal(ctx, sysAuthID, model.IntegrationSystemReference)
   771  		// THEN
   772  		require.NoError(t, err)
   773  		dbMock.AssertExpectations(t)
   774  	})
   775  
   776  	t.Run("Error when deleting application", func(t *testing.T) {
   777  		db, dbMock := testdb.MockDatabase(t)
   778  		ctx := persistence.SaveToContext(context.TODO(), db)
   779  
   780  		query := `DELETE FROM public.system_auths WHERE tenant_id = $1 AND id = $2 AND app_id IS NOT NULL`
   781  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   782  			WithArgs(testTenant, sysAuthID).
   783  			WillReturnError(testErr)
   784  
   785  		repo := systemauth.NewRepository(nil)
   786  		// WHEN
   787  		err := repo.DeleteByIDForObject(ctx, testTenant, sysAuthID, model.ApplicationReference)
   788  		// THEN
   789  		require.Error(t, err)
   790  		assert.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   791  		dbMock.AssertExpectations(t)
   792  	})
   793  }
   794  
   795  func TestRepository_Update(t *testing.T) {
   796  	sysAuthID := "foo"
   797  	objID := "bar"
   798  	modelAuth := fixModelAuth()
   799  
   800  	t.Run("success when updating systemAuth", func(t *testing.T) {
   801  		// GIVEN
   802  		db, dbMock := testdb.MockDatabase(t)
   803  		ctx := persistence.SaveToContext(context.TODO(), db)
   804  
   805  		query := `UPDATE public.system_auths SET value = ? WHERE id = ? AND tenant_id = ?`
   806  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   807  			WithArgs(testMarshalledSchema, sysAuthID, testTenant).
   808  			WillReturnResult(sqlmock.NewResult(-1, 1))
   809  
   810  		modelSysAuth := fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth)
   811  		entSysAuth := fixEntity(sysAuthID, model.RuntimeReference, objID, true)
   812  
   813  		convMock := &automock.Converter{}
   814  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
   815  		repo := systemauth.NewRepository(convMock)
   816  
   817  		// WHEN
   818  		err := repo.Update(ctx, modelSysAuth)
   819  
   820  		// THEN
   821  		assert.NoError(t, err)
   822  		dbMock.AssertExpectations(t)
   823  		convMock.AssertExpectations(t)
   824  	})
   825  
   826  	t.Run("error when updating systemAuth", func(t *testing.T) {
   827  		// GIVEN
   828  		db, dbMock := testdb.MockDatabase(t)
   829  		ctx := persistence.SaveToContext(context.TODO(), db)
   830  
   831  		query := `UPDATE public.system_auths SET value = ? WHERE id = ? AND tenant_id = ?`
   832  		dbMock.ExpectExec(regexp.QuoteMeta(query)).
   833  			WithArgs(testMarshalledSchema, sysAuthID, testTenant).
   834  			WillReturnError(testErr)
   835  
   836  		modelSysAuth := fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth)
   837  		entSysAuth := fixEntity(sysAuthID, model.RuntimeReference, objID, true)
   838  
   839  		convMock := &automock.Converter{}
   840  		convMock.On("ToEntity", *modelSysAuth).Return(entSysAuth, nil).Once()
   841  		repo := systemauth.NewRepository(convMock)
   842  
   843  		// WHEN
   844  		err := repo.Update(ctx, modelSysAuth)
   845  
   846  		// THEN
   847  		require.Error(t, err)
   848  		assert.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   849  		dbMock.AssertExpectations(t)
   850  		convMock.AssertExpectations(t)
   851  	})
   852  
   853  	t.Run("error when item is nil", func(t *testing.T) {
   854  		// GIVEN
   855  		repo := systemauth.NewRepository(nil)
   856  
   857  		// WHEN
   858  		err := repo.Update(context.TODO(), nil)
   859  
   860  		// THEN
   861  		require.Error(t, err)
   862  		assert.EqualError(t, err, "Internal Server Error: item cannot be nil")
   863  	})
   864  
   865  	t.Run("error when converter returns error", func(t *testing.T) {
   866  		// GIVEN
   867  		modelSysAuth := fixModelSystemAuth("foo", model.RuntimeReference, objID, modelAuth)
   868  		convMock := &automock.Converter{}
   869  		convMock.On("ToEntity", *modelSysAuth).Return(systemauth.Entity{}, testErr).Once()
   870  		repo := systemauth.NewRepository(convMock)
   871  
   872  		// WHEN
   873  		err := repo.Update(context.TODO(), modelSysAuth)
   874  
   875  		// THEN
   876  		require.Error(t, err)
   877  		assert.EqualError(t, err, "while converting model to entity: test error")
   878  		convMock.AssertExpectations(t)
   879  	})
   880  }
   881  
   882  func TestRepository_GetByJSONValue(t *testing.T) {
   883  	t.Run("error when GetGlobal fails", func(t *testing.T) {
   884  		// GIVEN
   885  		value := make(map[string]interface{})
   886  		value["key"] = "value"
   887  		db, dbMock := testdb.MockDatabase(t)
   888  		ctx := persistence.SaveToContext(context.TODO(), db)
   889  
   890  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE value @> $1`
   891  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   892  			WithArgs("{\"key\":\"value\"}").
   893  			WillReturnError(testErr)
   894  		repo := systemauth.NewRepository(nil)
   895  
   896  		// WHEN
   897  		sysAuth, err := repo.GetByJSONValue(ctx, value)
   898  
   899  		// THEN
   900  		require.Error(t, err)
   901  		assert.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query")
   902  		assert.Nil(t, sysAuth)
   903  		dbMock.AssertExpectations(t)
   904  	})
   905  
   906  	t.Run("error when converter fails", func(t *testing.T) {
   907  		// GIVEN
   908  		value := make(map[string]interface{})
   909  		value["key"] = "value"
   910  		db, dbMock := testdb.MockDatabase(t)
   911  		ctx := persistence.SaveToContext(context.TODO(), db)
   912  
   913  		saID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
   914  		objectID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
   915  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   916  
   917  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE value @> $1`
   918  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   919  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   920  
   921  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   922  			WithArgs("{\"key\":\"value\"}").
   923  			WillReturnRows(rows)
   924  		convMock := &automock.Converter{}
   925  		convMock.On("FromEntity", saEntity).Return(model.SystemAuth{}, testErr).Once()
   926  		repo := systemauth.NewRepository(convMock)
   927  
   928  		// WHEN
   929  		sysAuth, err := repo.GetByJSONValue(ctx, value)
   930  
   931  		// THEN
   932  		require.Error(t, err)
   933  		assert.EqualError(t, err, "while converting SystemAuth entity to model: test error")
   934  		assert.Nil(t, sysAuth)
   935  		dbMock.AssertExpectations(t)
   936  		convMock.AssertExpectations(t)
   937  	})
   938  
   939  	t.Run("success when getting by json value", func(t *testing.T) {
   940  		// GIVEN
   941  		value := make(map[string]interface{})
   942  		value["key"] = "value"
   943  		db, dbMock := testdb.MockDatabase(t)
   944  		ctx := persistence.SaveToContext(context.TODO(), db)
   945  
   946  		saID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
   947  		objectID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
   948  		saEntity := fixEntity(saID, model.RuntimeReference, objectID, true)
   949  
   950  		query := `SELECT id, tenant_id, app_id, runtime_id, integration_system_id, value FROM public.system_auths WHERE value @> $1`
   951  		rows := sqlmock.NewRows([]string{"id", "tenant_id", "app_id", "runtime_id", "integration_system_id", "value"}).
   952  			AddRow(saID, testTenant, saEntity.AppID, saEntity.RuntimeID, saEntity.IntegrationSystemID, saEntity.Value)
   953  
   954  		dbMock.ExpectQuery(regexp.QuoteMeta(query)).
   955  			WithArgs("{\"key\":\"value\"}").
   956  			WillReturnRows(rows)
   957  		convMock := &automock.Converter{}
   958  		expectedModel := model.SystemAuth{}
   959  		convMock.On("FromEntity", saEntity).Return(expectedModel, nil).Once()
   960  		repo := systemauth.NewRepository(convMock)
   961  
   962  		// WHEN
   963  		sysAuth, err := repo.GetByJSONValue(ctx, value)
   964  
   965  		// THEN
   966  		assert.Nil(t, err)
   967  		assert.Equal(t, &expectedModel, sysAuth)
   968  		dbMock.AssertExpectations(t)
   969  		convMock.AssertExpectations(t)
   970  	})
   971  }
   972  func givenError() error {
   973  	return errors.New("some error")
   974  }