Update to use a generic event emitter.
Rather can creating a purpose-built event emitter for each and every event we
need to emit (I'm looking at you, login verification event) which is _downright
silly_, we're now using a generic event publisher that's based on saying "HEY A
MODEL UPDATED".
This means we need to change all our setup code in authd to use
events.NewNSQPublisher or events.NewStdoutPublisher instead of our homegrown
solutions. Which also means updating our config to take an events.Publisher
instead of our LoginVerificationNotifier (blergh).
Our Context also now uses an events.Publisher instead of a
LoginVerificationNotifier. Party all around! We also replaced our
SendLoginVerification helper method on Context with a SendModelEvent helper
method on Context, which is just a light wrapper around
events.PublishModelEvent.
Of course, all this means we need to update our email_verification listener to
listen to the correct channel (based on the model we want updates about) and
filter down to a Created action or our new custom action for "the customer wants
their verification resent", which I'm OK making a special case and not generic,
because c'mon. But we had a subtle change to all our constants, some of which
are unofficial constants now. I'm unsure how I feel about this.
We also updated our email_verification listener so that we're unmarshalling to a
custom loginEvent, which is just an events.Event that overwrites the Data
property to be an auth.Login instance. This is to make sure we don't need to
wrangle a map[string]interface{}, which is no fun. I'm also OK with
special-casing like this, because it's 1) a tiny amount of code, 2) properly
utilising composition, and 3) the only way I can think of to cleanly accomplish
what I want.
I also added a note about GetLogin's deficient handling of logins, namely that
it doesn't recognise admins and return Verification codes to them, which would
be a useful property for internal tools to take advantage of. Ah well.
I updated the Profile and Login implementations so they're now event.Model
instances, mainly by just exporting some strings from them through getters that
will let us automatically build an Event from them. This lets us use the
PublishModelEvent helper.
I updated our CreateProfileHandler to properly mangle the login Verification
property, and to fire off the ActionCreated events for the new Login and the new
Profile.
I updated our GetLoginHandler and UpdateLoginHandler to properly mangle the
loginVerification property. God that's annoying. :-/
You'll note I didn't start publishing the events.ActionUpdated or
events.ActionDeleted events for Profiles or Logins yet, and didn't bother
publishing any events for literally any other type. That's because I'm a lazy
piece of crap and will end up publishing them when I absolutely have to. Part of
that is because if a channel isn't created/being read for a topic, the messages
will just stack up in NSQ, and I don't want that. But mostly I'm lazy.
Finally, I got to delete the entire profile_verification.go file, because we're
no longer special-casing that. Hooray!
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.TerminateSession(session.ID)
96 t.Errorf("Error terminating session in %T: %s", store, err)
98 retrieved, err = context.GetSession(session.ID)
100 t.Errorf("Error retrieving session from %T: %s", store, err)
103 expected.Active = false
104 success, field, expectation, result = compareSessions(expected, retrieved)
106 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
108 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
110 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
112 if len(retrievedList) != 1 {
113 t.Errorf("Expected 1 session retrieved by profile from %T, got %d", store, len(retrievedList))
115 success, field, expectation, result = compareSessions(expected, retrievedList[0])
117 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
119 err = context.RemoveSession(session.ID)
121 t.Errorf("Error removing session from %T: %s", store, err)
123 retrieved, err = context.GetSession(session.ID)
124 if err != ErrSessionNotFound {
125 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
127 retrievedList, err = context.ListSessions(session.ProfileID, time.Time{}, 10)
129 t.Errorf("Error retrieving sessions by profile from %T: %s", store, err)
131 if len(retrievedList) != 0 {
132 t.Errorf("Expected 0 sessions retrieved by profile from %T, got %d", store, len(retrievedList))
134 err = context.RemoveSession(session.ID)
135 if err != ErrSessionNotFound {
136 t.Errorf("Expected ErrSessionNotFound from %T, got %s", store, err)
138 err = context.TerminateSession(session.ID)
139 if err != ErrSessionNotFound {
140 t.Errorf("Expected ERrSessionNotFound from %T, got %s", store, err)
145 // BUG(paddy): We need to test the CreateSessionHandler.
146 // BUG(paddy): We need to test the credentialsValidate function.