github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/oauth2/in_memory_repository_test.go (about)

     1  package oauth2
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hellofresh/janus/pkg/proxy"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestOauthServerInMemoryRepository(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := []struct {
    14  		scenario string
    15  		function func(*testing.T, Repository)
    16  	}{
    17  		{
    18  			scenario: "remove existent oauth server",
    19  			function: testRemoveExistentOAuthServer,
    20  		},
    21  		{
    22  			scenario: "remove inexistent oauth server",
    23  			function: testRemoveInexistentOAuthServer,
    24  		},
    25  		{
    26  			scenario: "find all oauth servers",
    27  			function: testFindAllOAuthServers,
    28  		},
    29  		{
    30  			scenario: "find by name",
    31  			function: testFindByName,
    32  		},
    33  		{
    34  			scenario: "not find by name",
    35  			function: testNotFindByName,
    36  		},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		t.Run(test.scenario, func(t *testing.T) {
    41  			repo := newInMemoryRepo()
    42  			test.function(t, repo)
    43  		})
    44  	}
    45  }
    46  
    47  func testRemoveExistentOAuthServer(t *testing.T, repo Repository) {
    48  	err := repo.Remove("test1")
    49  	assert.NoError(t, err)
    50  }
    51  
    52  func testRemoveInexistentOAuthServer(t *testing.T, repo Repository) {
    53  	err := repo.Remove("test")
    54  	assert.Error(t, err)
    55  }
    56  
    57  func testFindAllOAuthServers(t *testing.T, repo Repository) {
    58  	results, err := repo.FindAll()
    59  	assert.NoError(t, err)
    60  	assert.Len(t, results, 2)
    61  }
    62  
    63  func testFindByName(t *testing.T, repo Repository) {
    64  	definition, err := repo.FindByName("test1")
    65  	assert.NoError(t, err)
    66  	assert.NotNil(t, definition)
    67  }
    68  
    69  func testNotFindByName(t *testing.T, repo Repository) {
    70  	definition, err := repo.FindByName("invalid")
    71  	assert.Error(t, err)
    72  	assert.Nil(t, definition)
    73  }
    74  
    75  func newInMemoryRepo() *InMemoryRepository {
    76  	repo := NewInMemoryRepository()
    77  	repo.Add(&OAuth{
    78  		Name: "test1",
    79  		Endpoints: Endpoints{
    80  			Token: &proxy.Definition{
    81  				ListenPath: "/token",
    82  				Upstreams: &proxy.Upstreams{
    83  					Balancing: "roundrobin",
    84  					Targets: []*proxy.Target{
    85  						{Target: "http://test.com/token"},
    86  					},
    87  				},
    88  			},
    89  		},
    90  	})
    91  
    92  	repo.Add(&OAuth{
    93  		Name: "test2",
    94  		Endpoints: Endpoints{
    95  			Token: &proxy.Definition{
    96  				ListenPath: "/token",
    97  				Upstreams: &proxy.Upstreams{
    98  					Balancing: "roundrobin",
    99  					Targets: []*proxy.Target{
   100  						{Target: "http://test2.com/token"},
   101  					},
   102  				},
   103  			},
   104  		},
   105  	})
   106  
   107  	return repo
   108  }