internal/frontend/assets/html: make timestamp human readable (#1107)

Since we switch to use databroker, time in template is now protobuf
timestamp instead of time.Time, that causes it appears in raw form
instead of human-readable format.

Fix this by converting protobuf timestamp to time.Time in template.
There's still a breaking change, though. The time will now appears in
UTC instead of local time.

Fixes #1100
This commit is contained in:
Cuong Manh Le 2020-07-20 13:35:57 +07:00 committed by GitHub
parent 0f17fb0d95
commit 9dae633fe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 5 deletions

View file

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"strings"
"testing" "testing"
"time" "time"
@ -527,6 +528,81 @@ func TestJwksEndpoint(t *testing.T) {
expected := `{"keys":[{"use":"sig","kty":"EC","kid":"5b419ade1895fec2d2def6cd33b1b9a018df60db231dc5ecb85cbed6d942813c","crv":"P-256","alg":"ES256","x":"UG5xCP0JTT1H6Iol8jKuTIPVLM04CgW9PlEypNRmWlo","y":"KChF0fR09zm884ymInM29PtSsFdnzExNfLsP-ta1AgQ"}]}` expected := `{"keys":[{"use":"sig","kty":"EC","kid":"5b419ade1895fec2d2def6cd33b1b9a018df60db231dc5ecb85cbed6d942813c","crv":"P-256","alg":"ES256","x":"UG5xCP0JTT1H6Iol8jKuTIPVLM04CgW9PlEypNRmWlo","y":"KChF0fR09zm884ymInM29PtSsFdnzExNfLsP-ta1AgQ"}]}`
assert.Equal(t, body, expected) assert.Equal(t, body, expected)
} }
func TestAuthenticate_Dashboard(t *testing.T) {
t.Parallel()
now := time.Now()
pbNow, _ := ptypes.TimestampProto(now)
nowStr := now.UTC().Format("2006-01-02 15:04:05.999999999")
tests := []struct {
name string
method string
sessionStore sessions.SessionStore
wantCode int
wantBody string
}{
{"good", http.MethodGet, &mstore.Store{Encrypted: true, Session: &sessions.State{ID: "SESSION_ID", IssuedAt: jwt.NewNumericDate(now)}}, http.StatusOK, ""},
{"good with expected timestamp format", http.MethodGet, &mstore.Store{Encrypted: true, Session: &sessions.State{ID: "SESSION_ID", IssuedAt: jwt.NewNumericDate(now)}}, http.StatusOK, nowStr},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
signer, err := jws.NewHS256Signer(nil, "mock")
if err != nil {
t.Fatal(err)
}
a := &Authenticate{
sessionStore: tt.sessionStore,
encryptedEncoder: signer,
sharedEncoder: signer,
templates: template.Must(frontend.NewTemplates()),
dataBrokerClient: mockDataBrokerServiceClient{
get: func(ctx context.Context, in *databroker.GetRequest, opts ...grpc.CallOption) (*databroker.GetResponse, error) {
data, err := ptypes.MarshalAny(&session.Session{
Id: "SESSION_ID",
UserId: "USER_ID",
IdToken: &session.IDToken{IssuedAt: pbNow},
})
if err != nil {
return nil, err
}
return &databroker.GetResponse{
Record: &databroker.Record{
Version: "0001",
Type: data.GetTypeUrl(),
Id: "SESSION_ID",
Data: data,
},
}, nil
},
},
}
u, _ := url.Parse("/")
r := httptest.NewRequest(tt.method, u.String(), nil)
state, err := tt.sessionStore.LoadSession(r)
if err != nil {
t.Fatal(err)
}
ctx := r.Context()
ctx = sessions.NewContext(ctx, state, nil)
r = r.WithContext(ctx)
r.Header.Set("Accept", "application/json")
w := httptest.NewRecorder()
httputil.HandlerFunc(a.Dashboard).ServeHTTP(w, r)
if status := w.Code; status != tt.wantCode {
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.wantCode)
}
body := w.Body.String()
if !strings.Contains(body, tt.wantBody) {
t.Errorf("Unexpected body, contains: %s, got: %s", tt.wantBody, body)
}
})
}
}
type mockDataBrokerServiceClient struct { type mockDataBrokerServiceClient struct {
databroker.DataBrokerServiceClient databroker.DataBrokerServiceClient

View file

@ -110,8 +110,8 @@
<input <input
type="text" type="text"
class="field" class="field"
value="{{.}}" value="{{.AsTime}}"
title="{{.}}" title="{{.AsTime}}"
disabled disabled
/> />
</label> </label>
@ -122,8 +122,8 @@
<input <input
type="text" type="text"
class="field" class="field"
value="{{.}}" value="{{.AsTime}}"
title="{{.}}" title="{{.AsTime}}"
disabled disabled
/> />
</label> </label>

File diff suppressed because one or more lines are too long