Implement postgres version of the tokenStore.
Create a postgres implementation for the tokenStore. Note that because pq
doesn't support Postgres' array types (see https://github.com/lib/pq/issues/49),
we couldn't just store the token.Scopes field as a Postgres array of varchars,
which would have been the ideal. Instead, we need a many-to-many table that maps
tokens to scopes. This meant we needed a special tokenScope type for our
database mapping, and we needed to complicate the token storage/retrieval
functions a little bit. It's kind of ugly, I'm not a huge fan of it, and I'd
much rather be using the Postgres array types, but... well, here we are.
We also added the postgres tokenStore to our slice of tokenStores to test when
the correct environment variables are present.
We wrote initialization SQL for the tables required by the postgres tokenStore.
Also, added a helper script for emptying the test database, because I got tired
of doing it by hand. We should be doing that in an automated fashion in the
tests themselves, but that would mean extending the *Store interfaces.
8 "code.secondbit.org/uuid.hg"
12 if os.Getenv("PG_TEST_DB") != "" {
13 p, err := NewPostgres(os.Getenv("PG_TEST_DB"))
17 sessionStores = append(sessionStores, &p)
21 var sessionStores = []sessionStore{NewMemstore()}
23 func compareSessions(session1, session2 Session) (success bool, field string, val1, val2 interface{}) {
24 if session1.ID != session2.ID {
25 return false, "ID", session1.ID, session2.ID
27 if session1.IP != session2.IP {
28 return false, "IP", session1.IP, session2.IP
30 if session1.UserAgent != session2.UserAgent {
31 return false, "UserAgent", session1.UserAgent, session2.UserAgent
33 if !session1.ProfileID.Equal(session2.ProfileID) {
34 return false, "ProfileID", session1.ProfileID, session2.ProfileID
36 if !session1.Created.Equal(session2.Created) {
37 return false, "Created", session1.Created, session2.Created
39 if !session1.Expires.Equal(session2.Expires) {
40 return false, "Expires", session1.Expires, session2.Expires
42 if session1.Login != session2.Login {
43 return false, "Login", session1.Login, session2.Login
45 if session1.Active != session2.Active {
46 return false, "Active", session1.Active, session2.Active
48 if session1.CSRFToken != session2.CSRFToken {
49 return false, "CSRFToken", session1.CSRFToken, session2.CSRFToken
51 return true, "", nil, nil
54 func TestSessionStoreSuccess(t *testing.T) {
57 ID: uuid.NewID().String() + uuid.NewID().String(),
59 UserAgent: "TestRunner",
60 ProfileID: uuid.NewID(),
61 Created: time.Now().Round(time.Millisecond),
62 Login: "test@example.com",
65 for _, store := range sessionStores {
66 context := Context{sessions: store}
67 err := context.CreateSession(session)
69 t.Errorf("Error saving session to %T: %s", store, err)
71 err = context.CreateSession(session)
72 if err != ErrSessionAlreadyExists {
73 t.Errorf("Expected ErrSessionAlreadyExists from %T, got %s", store, err)
75 retrieved, err := context.GetSession(session.ID)
77 t.Errorf("Error retrieving session from %T: %s", store, err)
79 success, field, expectation, result := compareSessions(session, retrieved)
81 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
83 retrievedList, err := context.ListSessions(session.ProfileID, time.Time{}, 10)
85 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
87 if len(retrievedList) != 1 {
88 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
90 success, field, expectation, result = compareSessions(session, retrievedList[0])
92 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
94 err = context.RemoveSession(session.ID)
96 t.Errorf("Error removing session from %T: %s", store, err)
98 retrieved, err = context.GetSession(session.ID)
99 if err != ErrSessionNotFound {
100 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
102 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
104 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
106 if len(retrievedList) != 0 {
107 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
109 err = context.RemoveSession(session.ID)
110 if err != ErrSessionNotFound {
111 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
116 // BUG(paddy): We need to test the CreateSessionHandler.
117 // BUG(paddy): We need to test the credentialsValidate function.