Skip to main content

Device Service Tests

Overview​

Integration tests for the device-service, testing complete device management flows against a live service.

Running Tests​

cd /backend
make test device

Test Configuration​

  • Auth URL: http://localhost:80/api/auth (via gateway)
  • Device URL: http://localhost:80/api/devices (via gateway)
  • Database: Direct PostgreSQL connection for test setup/cleanup
  • Email Domain: @pytest.example.com (generated per test)

Test Flows​

Test ClassDescription
TestAdminDeviceCrudhealth β†’ create β†’ list β†’ get β†’ update β†’ delete (admin)
TestRegularUserCannotCreateuser cannot create devices (403)
TestRegularUserCannotDeleteuser cannot delete devices (403)
TestDuplicateUniqueIdduplicate uniqueId returns 409 Conflict
TestValidateTokenvalidate token β†’ invalid token β†’ claimed token (admin only)
TestClaimDeviceclaim β†’ list β†’ get β†’ verify ownership and permissions
TestClaimAlreadyClaimedclaim same token twice β†’ alreadyClaimed=true
TestClaimByOtherUserclaim by second user β†’ 409 Conflict
TestUnclaimDeviceunclaim β†’ device removed from list
TestReclaimAfterUnclaimunclaim β†’ reclaim same token succeeds
TestUserCannotAccessOthersDeviceuser cannot get another user's device (403)
TestUnauthenticatedAccesslist without auth β†’ 401
TestHealthhealth check returns 200

Helper Functions​

Located in tests/helpers.py:

Database Setup​

FunctionDescription
create_admin_user(email?, password?)Create admin user directly in DB
create_regular_user(email?, password?)Create regular user directly in DB
delete_user(user_id)Delete user and their device links
create_device_token(unique_id?, model?)Create device claim token in DB
delete_device_token(token_id)Delete token from device_list
delete_device_by_unique_id(unique_id)Delete device and its user links

Authentication​

FunctionDescription
login_user(auth_client, email, password)Login via auth API, returns access token
get_auth_headers(access_token)Create Authorization: Bearer header

Device API Calls​

FunctionDescription
check_health(client)Verify service is healthy
list_devices(client, headers)GET / - list user's devices
get_device(client, headers, device_id)GET /{id} - get device details
create_device(client, headers, name, unique_id, **kwargs)POST / - create device (admin)
update_device(client, headers, device_id, **kwargs)PUT /{id} - update device
delete_device(client, headers, device_id)DELETE /{id} - delete device (admin)
validate_token(client, headers, token)POST /validate-token - check token (admin)
claim_device(client, headers, token)POST /claim - claim device
unclaim_device(client, headers, device_id)POST /unclaim/{id} - unclaim device

Test Strategy​

Tests create users and device tokens directly in the database (not via API) to:

  1. Ensure test independence from auth-service registration flow
  2. Avoid email verification requirements
  3. Allow testing both admin and regular user roles

Users are then authenticated via the auth-service login API to obtain real JWT tokens for device API calls.

Permissions Tested​

OperationAdminOwnerShared UserOther User
Create Deviceβœ…βŒβŒβŒ
Delete Deviceβœ…βŒβŒβŒ
Update Deviceβœ…βœ…βŒβŒ
View Deviceβœ…βœ…βœ…βŒ
Claim Deviceβœ…βœ…βœ…βœ…
Unclaim DeviceN/Aβœ…βœ…βŒ
Validate Tokenβœ…βŒβŒβŒ