state: infer user from subject (#772)

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2020-05-26 10:31:55 -07:00 committed by Bobby DeSimone
parent ad79585ae8
commit 7ccd364c7e
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
3 changed files with 43 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package sessions
import (
"encoding/json"
"strings"
"testing"
"time"
@ -144,3 +145,41 @@ func TestState_accessTokenHash(t *testing.T) {
})
}
}
func TestState_UnmarshalJSON(t *testing.T) {
fixedTime := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
timeNow = func() time.Time {
return fixedTime
}
defer func() { timeNow = time.Now }()
tests := []struct {
name string
in *State
want State
wantErr bool
}{
{"good", &State{}, State{}, false},
{"with user", &State{User: "user"}, State{User: "user"}, false},
{"without", &State{Subject: "user"}, State{User: "user", Subject: "user"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.in)
if err != nil {
t.Fatal(err)
}
s := &State{}
if err := s.UnmarshalJSON(data); (err != nil) != tt.wantErr {
t.Errorf("State.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
got := *s
cmpOpts := []cmp.Option{
cmpopts.IgnoreUnexported(State{}),
}
if diff := cmp.Diff(got, tt.want, cmpOpts...); diff != "" {
t.Errorf("State.UnmarshalJSON() error = %v", diff)
}
})
}
}