This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/storage/storagetest/storagetest.go

82 lines
2.2 KiB
Go
Raw Normal View History

2016-07-25 20:00:28 +00:00
// +build go1.7
// Package storagetest provides conformance tests for storage implementations.
package storagetest
import (
"reflect"
"testing"
"time"
2016-08-11 05:31:42 +00:00
"github.com/coreos/dex/storage"
2016-07-25 20:00:28 +00:00
)
var neverExpire = time.Now().Add(time.Hour * 24 * 365 * 100)
// RunTestSuite runs a set of conformance tests against a storage.
func RunTestSuite(t *testing.T, s storage.Storage) {
t.Run("UpdateAuthRequest", func(t *testing.T) { testUpdateAuthRequest(t, s) })
t.Run("CreateRefresh", func(t *testing.T) { testCreateRefresh(t, s) })
}
func testUpdateAuthRequest(t *testing.T, s storage.Storage) {
a := storage.AuthRequest{
2016-08-03 04:57:36 +00:00
ID: storage.NewID(),
2016-07-25 20:00:28 +00:00
ClientID: "foobar",
ResponseTypes: []string{"code"},
Scopes: []string{"openid", "email"},
RedirectURI: "https://localhost:80/callback",
Expiry: neverExpire,
}
2016-08-03 04:57:36 +00:00
identity := storage.Claims{Email: "foobar"}
2016-07-25 20:00:28 +00:00
if err := s.CreateAuthRequest(a); err != nil {
t.Fatalf("failed creating auth request: %v", err)
}
if err := s.UpdateAuthRequest(a.ID, func(old storage.AuthRequest) (storage.AuthRequest, error) {
old.Claims = identity
2016-07-25 20:00:28 +00:00
old.ConnectorID = "connID"
return old, nil
}); err != nil {
t.Fatalf("failed to update auth request: %v", err)
}
got, err := s.GetAuthRequest(a.ID)
if err != nil {
t.Fatalf("failed to get auth req: %v", err)
}
if !reflect.DeepEqual(got.Claims, identity) {
t.Fatalf("update failed, wanted identity=%#v got %#v", identity, got.Claims)
2016-07-25 20:00:28 +00:00
}
}
func testCreateRefresh(t *testing.T, s storage.Storage) {
2016-08-03 04:57:36 +00:00
id := storage.NewID()
refresh := storage.RefreshToken{
2016-07-25 20:00:28 +00:00
RefreshToken: id,
ClientID: "client_id",
ConnectorID: "client_secret",
Scopes: []string{"openid", "email", "profile"},
}
if err := s.CreateRefresh(refresh); err != nil {
t.Fatalf("create refresh token: %v", err)
}
gotRefresh, err := s.GetRefresh(id)
if err != nil {
t.Fatalf("get refresh: %v", err)
}
if !reflect.DeepEqual(gotRefresh, refresh) {
t.Errorf("refresh returned did not match expected")
}
if err := s.DeleteRefresh(id); err != nil {
t.Fatalf("failed to delete refresh request: %v", err)
}
if _, err := s.GetRefresh(id); err != storage.ErrNotFound {
t.Errorf("after deleting refresh expected storage.ErrNotFound, got %v", err)
}
}