diff --git a/backend/server/server.go b/backend/server/server.go index cc9a4be..9934d52 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -85,6 +85,23 @@ func apiSubmitHandler(w http.ResponseWriter, r *http.Request) { } } +func apiEBootstrapHandler(w http.ResponseWriter, r *http.Request) { + userId := r.URL.Query().Get("user_id") + deviceId := r.URL.Query().Get("device_id") + updateUsageData(userId, deviceId) + tx := GLOBAL_DB.Where("user_id = ?", userId) + var historyEntries []*shared.EncHistoryEntry + result := tx.Find(&historyEntries) + if result.Error != nil { + panic(fmt.Errorf("DB query error: %v", result.Error)) + } + resp, err := json.Marshal(historyEntries) + if err != nil { + panic(err) + } + w.Write(resp) +} + func apiQueryHandler(w http.ResponseWriter, r *http.Request) { userId := r.URL.Query().Get("user_id") deviceId := r.URL.Query().Get("device_id") diff --git a/backend/server/server_test.go b/backend/server/server_test.go index 91007d3..a6c2ab5 100644 --- a/backend/server/server_test.go +++ b/backend/server/server_test.go @@ -97,6 +97,19 @@ func TestESubmitThenQuery(t *testing.T) { if !data.EntryEquals(decEntry, entry) { t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, entry) } + + // Bootstrap handler should return 2 entries, one for each device + w = httptest.NewRecorder() + searchReq = httptest.NewRequest(http.MethodGet, "/?user_id="+data.UserId("key")+"&device_id="+devId1, nil) + apiEBootstrapHandler(w, searchReq) + res = w.Result() + defer res.Body.Close() + respBody, err = ioutil.ReadAll(res.Body) + shared.Check(t, err) + shared.Check(t, json.Unmarshal(respBody, &retrievedEntries)) + if len(retrievedEntries) != 2 { + t.Fatalf("Expected to retrieve 2 entries, found %d", len(retrievedEntries)) + } } func TestDumpRequestAndResponse(t *testing.T) {