package auth

import (
	"testing"
	"time"

	"secondbit.org/uuid"
)

var grantStores = []GrantStore{NewMemstore()}

func TestGrantStoreSuccess(t *testing.T) {
	grant := Grant{
		Code:        "code",
		Created:     time.Now(),
		ExpiresIn:   180,
		ClientID:    uuid.NewID(),
		Scope:       "scope",
		RedirectURI: "redirectURI",
		State:       "state",
	}
	for pos, store := range grantStores {
		err := store.SaveGrant(grant)
		if err != nil {
			t.Errorf("Error saving grant in GrantStore #%d: %s", pos, err)
		}
		retrieved, err := store.GetGrant(grant.Code)
		if err != nil {
			t.Errorf("Error retrieving grant in GrantStore #%d: %s", pos, err)
		}
		t.Log(retrieved)
		// TODO: compare retrieved to grant
		err = store.DeleteGrant(grant.Code)
		if err != nil {
			t.Errorf("Error removing grant from GrantStore #%d: %s", pos, err)
		}
		retrieved, err = store.GetGrant(grant.Code)
		if err != ErrGrantNotFound {
			t.Errorf("Expected ErrGrantNotFound from GrantStore #%d, got %+v and %+v", pos, retrieved, err)
		}
	}
}
