Add updating devices to apiv1.
We needed a way to be able to update devices after they were created. This is
supported in the devices package, we just needed to expose it using apiv1
endpoints.
In doing so, it became apparent that allowing users to change the Owner of their
Devices wasn't properly thought through, and pending a reason to use it, I'm
just removing it. The biggest issue came when trying to return usable error
messages; we couldn't distinguish between "you don't own the device you're
trying to update" and "you're not allowed to change the owner of the device". I
also couldn't figure out _who should be able to_ change the owner of the device,
which is generally an indication that I'm building a feature before I have a use
case for it.
To support this change, the apiv1.DeviceChange type needed its Owner property
removed.
I also needed to add deviceFromAPI and devicesFromAPI helpers to return
devices.Device types from apiv1.Device types.
There's now a new validateDeviceUpdate helper that checks to ensure that a
device update request is valid and the user has the appropriate permissions.
The createRequest type now accepts a slice of Devices, not a slice of
DeviceChanges, because we want to pass the Owner in.
A new updateRequest type is created, which accepts a DeviceChange to apply.
A new handleUpdateDevice handler is created, which is assigned to the endpoint
for PATCH requests against a device ID. It checks that the user is logged in,
the Device they're trying to update exists, and that it's a valid update. If all
of that is true, the device is updated and the updated device is returned.
Finally, we had to add two new scopes to support new functionality:
ScopeUpdateOtherUserDevices allows a user to update other user's devices, and
ScopeUpdateLastSeen allows a user to update the LastSeen property of a device.
Pending some better error messages, this should be a full implementation of
updating a device, which leaves only the deletion endpoint to deal with.
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)