Add endpoint for retrieving devices.
Add an endpoint for retrieving devices, either as a list or by ID.
Stub endpoints for updating and deleting devices., along with TODOs marking them
as things to still be completed. (Right now, accessing those endpoints is an
insta-panic.)
Simplify our handleCreateDevices by returning StatusUnauthorized if AuthUser
fails, so we can reserve StatusForbidden for when auth succeeds but access is
still denied. Also, delay the instantiation and allocation of a Response
variable until we're actually going to use it.
Create a handleGetDevices handler that authenticates the user, and if no ID is
set, returns a list of all their Devices. If one or more IDs are set, only those
Devices are returned. If ScopeViewPushToken is one of the scopes associated with
the request, the push tokens for each Device will be included in the response.
Otherwise, they will be omitted.
7 "code.secondbit.org/uuid.hg"
10 func TestToMap(t *testing.T) {
12 {ID: uuid.NewID(), Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
13 {ID: uuid.NewID(), Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
14 {ID: uuid.NewID(), Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
16 expectation := map[string]Device{
17 devices[0].ID.String(): devices[0],
18 devices[1].ID.String(): devices[1],
19 devices[2].ID.String(): devices[2],
21 result := ToMap(devices)
23 if len(expectation) != len(result) {
24 t.Errorf("Expected %d devices in map, got %d\n", len(expectation), len(result))
27 for _, device := range devices {
28 ok, field, exp, res := compareDevices(expectation[device.ID.String()], result[device.ID.String()])
30 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, device.Name, exp, res)
35 func TestToSlice(t *testing.T) {
36 id1, id2, id3 := uuid.NewID(), uuid.NewID(), uuid.NewID()
37 devices := map[string]Device{
38 id1.String(): {ID: id1, Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
39 id2.String(): {ID: id2, Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
40 id3.String(): {ID: id3, Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
42 expectation := []Device{devices[id1.String()], devices[id2.String()], devices[id3.String()]}
43 result := ToSlice(devices)
45 if len(expectation) != len(result) {
46 t.Errorf("Expected %d devices in slice, got %d\n", len(expectation), len(result))
49 for _, device := range expectation {
51 for _, d := range result {
52 if !d.ID.Equal(device.ID) {
56 ok, field, exp, res := compareDevices(device, d)
58 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, d.Name, exp, res)
62 t.Errorf("Expected to find %s in result, did not\n", device.Name)