2023-06-17 17:35:17 +03:00
|
|
|
package teststore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIdentityProviderStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := NewTestingStore(ctx, t)
|
2023-06-26 18:46:01 +03:00
|
|
|
createdIDP, err := ts.CreateIdentityProvider(ctx, &store.IdentityProvider{
|
2023-06-17 17:35:17 +03:00
|
|
|
Name: "GitHub OAuth",
|
2023-07-02 13:56:25 +03:00
|
|
|
Type: store.IdentityProviderOAuth2Type,
|
2023-06-17 17:35:17 +03:00
|
|
|
IdentifierFilter: "",
|
|
|
|
Config: &store.IdentityProviderConfig{
|
|
|
|
OAuth2Config: &store.IdentityProviderOAuth2Config{
|
2023-06-26 18:46:01 +03:00
|
|
|
ClientID: "client_id",
|
|
|
|
ClientSecret: "client_secret",
|
2023-06-17 17:35:17 +03:00
|
|
|
AuthURL: "https://github.com/auth",
|
|
|
|
TokenURL: "https://github.com/token",
|
|
|
|
UserInfoURL: "https://github.com/user",
|
|
|
|
Scopes: []string{"login"},
|
|
|
|
FieldMapping: &store.FieldMapping{
|
|
|
|
Identifier: "login",
|
|
|
|
DisplayName: "name",
|
|
|
|
Email: "emai",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-26 18:46:01 +03:00
|
|
|
idp, err := ts.GetIdentityProvider(ctx, &store.FindIdentityProvider{
|
2023-06-17 17:35:17 +03:00
|
|
|
ID: &createdIDP.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 17:53:38 +03:00
|
|
|
require.NotNil(t, idp)
|
2023-06-17 17:35:17 +03:00
|
|
|
require.Equal(t, createdIDP, idp)
|
2023-06-26 18:46:01 +03:00
|
|
|
newName := "My GitHub OAuth"
|
|
|
|
updatedIdp, err := ts.UpdateIdentityProvider(ctx, &store.UpdateIdentityProvider{
|
|
|
|
ID: idp.ID,
|
|
|
|
Name: &newName,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newName, updatedIdp.Name)
|
|
|
|
err = ts.DeleteIdentityProvider(ctx, &store.DeleteIdentityProvider{
|
2023-06-17 17:35:17 +03:00
|
|
|
ID: idp.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-26 18:46:01 +03:00
|
|
|
idpList, err := ts.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
|
2023-06-17 17:35:17 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(idpList))
|
|
|
|
}
|