idp_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/usememos/memos/store"
  7. )
  8. func TestIdentityProviderStore(t *testing.T) {
  9. ctx := context.Background()
  10. ts := NewTestingStore(ctx, t)
  11. createdIDP, err := ts.CreateIdentityProvider(ctx, &store.IdentityProvider{
  12. Name: "GitHub OAuth",
  13. Type: store.IdentityProviderOAuth2Type,
  14. IdentifierFilter: "",
  15. Config: &store.IdentityProviderConfig{
  16. OAuth2Config: &store.IdentityProviderOAuth2Config{
  17. ClientID: "client_id",
  18. ClientSecret: "client_secret",
  19. AuthURL: "https://github.com/auth",
  20. TokenURL: "https://github.com/token",
  21. UserInfoURL: "https://github.com/user",
  22. Scopes: []string{"login"},
  23. FieldMapping: &store.FieldMapping{
  24. Identifier: "login",
  25. DisplayName: "name",
  26. Email: "emai",
  27. },
  28. },
  29. },
  30. })
  31. require.NoError(t, err)
  32. idp, err := ts.GetIdentityProvider(ctx, &store.FindIdentityProvider{
  33. ID: &createdIDP.ID,
  34. })
  35. require.NoError(t, err)
  36. require.NotNil(t, idp)
  37. require.Equal(t, createdIDP, idp)
  38. newName := "My GitHub OAuth"
  39. updatedIdp, err := ts.UpdateIdentityProvider(ctx, &store.UpdateIdentityProvider{
  40. ID: idp.ID,
  41. Name: &newName,
  42. })
  43. require.NoError(t, err)
  44. require.Equal(t, newName, updatedIdp.Name)
  45. err = ts.DeleteIdentityProvider(ctx, &store.DeleteIdentityProvider{
  46. ID: idp.ID,
  47. })
  48. require.NoError(t, err)
  49. idpList, err := ts.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
  50. require.NoError(t, err)
  51. require.Equal(t, 0, len(idpList))
  52. }