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!
7 if os.Getenv("PG_TEST_DB") != "" {
8 p, err := NewPostgres(os.Getenv("PG_TEST_DB"))
12 scopeStores = append(scopeStores, &p)
16 var scopeStores = []scopeStore{NewMemstore()}
18 func compareScopes(scope1, scope2 Scope) (success bool, field string, val1, val2 interface{}) {
19 if scope1.ID != scope2.ID {
20 return false, "ID", scope1.ID, scope2.ID
22 if scope1.Name != scope2.Name {
23 return false, "Name", scope1.Name, scope2.Name
25 if scope1.Description != scope2.Description {
26 return false, "Description", scope1.Description, scope2.Description
28 return true, "", nil, nil
31 func TestScopeInScopeStore(t *testing.T) {
35 Description: "Access to testing data.",
40 Description: "Access to minions.",
45 Description: "Access to bananas.",
47 updatedName := "Updated Scope"
48 updatedDescription := "An updated scope."
49 update := ScopeChange{
52 update2 := ScopeChange{
53 Description: &updatedDescription,
55 update3 := ScopeChange{
57 Description: &updatedDescription,
59 for _, store := range scopeStores {
60 context := Context{scopes: store}
61 retrieved, err := context.GetScopes([]string{scope.ID})
62 if len(retrieved) != 0 {
63 t.Logf("%+v", retrieved)
64 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
66 err = context.CreateScopes([]Scope{scope})
68 t.Errorf("Error saving scope to %T: %s", store, err)
70 err = context.CreateScopes([]Scope{scope})
71 if err != ErrScopeAlreadyExists {
72 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store)
74 retrieved, err = context.GetScopes([]string{scope.ID})
76 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
78 if len(retrieved) != 1 {
79 t.Logf("%+v", retrieved)
80 t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store)
82 success, field, val1, val2 := compareScopes(scope, retrieved[0])
84 t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store)
86 err = context.CreateScopes([]Scope{scope2, scope3})
88 t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err)
90 retrieved, err = context.GetScopes([]string{scope.ID, scope2.ID, scope3.ID})
92 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
94 if len(retrieved) != 3 {
95 t.Logf("%+v", retrieved)
96 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
98 for pos, s := range []Scope{scope, scope2, scope3} {
99 success, field, val1, val2 = compareScopes(s, retrieved[pos])
101 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
104 err = context.UpdateScope(scope.ID, update)
106 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
108 scope.ApplyChange(update)
109 err = context.UpdateScope(scope2.ID, update2)
111 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
113 scope2.ApplyChange(update2)
114 err = context.UpdateScope(scope3.ID, update3)
116 t.Errorf("Unexpected error updating scope in %T: %+v", store, err)
118 scope3.ApplyChange(update3)
119 retrieved, err = context.ListScopes()
121 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
123 if len(retrieved) != 3 {
124 t.Logf("%+v", retrieved)
125 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store)
127 for pos, s := range []Scope{scope, scope2, scope3} {
128 success, field, val1, val2 = compareScopes(s, retrieved[pos])
130 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store)
133 err = context.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID})
135 t.Errorf("Unexpected error removing scopes from %T: %+v", store, err)
137 retrieved, err = context.ListScopes()
139 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err)
141 if len(retrieved) != 0 {
142 t.Logf("%+v", retrieved)
143 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store)
145 err = context.RemoveScopes([]string{scope.ID})
147 t.Errorf("No error returned removing non-existent scopes from %T", store)
149 if err != ErrScopeNotFound {
150 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err)
152 err = context.UpdateScope(scope.ID, update)
153 if err != ErrScopeNotFound {
154 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err)