Skip to content

Commit 3055ea8

Browse files
fix: exclude removed devices from DeviceConflicts checks
When renaming or updating a device, the DeviceConflicts function was incorrectly detecting conflicts with devices that have status "removed". This prevented reusing device names from soft-deleted devices. Changes: - Modified DeviceConflicts to filter out devices with status "removed" - Added test case to verify removed devices don't cause conflicts - Added fixture for a removed device in test data
1 parent 6bfc916 commit 3055ea8

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

api/store/mongo/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (s *Store) DeviceConflicts(ctx context.Context, target *models.DeviceConfli
225225
"$or": []bson.M{
226226
{"name": target.Name},
227227
},
228+
"status": bson.M{"$ne": models.DeviceStatusRemoved},
228229
},
229230
},
230231
}

api/store/mongo/device_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,12 @@ func TestDeviceConflicts(t *testing.T) {
775775
fixtures: []string{fixtureDevices},
776776
expected: Expected{[]string{"name"}, true, nil},
777777
},
778+
{
779+
description: "no conflict with removed device name",
780+
target: &models.DeviceConflicts{Name: "device-removed"},
781+
fixtures: []string{fixtureDevicesWithRemoved},
782+
expected: Expected{[]string{}, false, nil},
783+
},
778784
}
779785

780786
for _, tc := range cases {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"devices": {
3+
"656f605bafb652df9927adef": {
4+
"created_at": "2023-01-01T12:00:00.000Z",
5+
"removed_at": null,
6+
"last_seen": "2023-01-01T12:00:00.000Z",
7+
"disconnected_at": null,
8+
"status_updated_at": "2023-01-01T12:00:00.000Z",
9+
"identity": {
10+
"mac": "mac-1"
11+
},
12+
"info": null,
13+
"name": "device-1",
14+
"position": null,
15+
"public_key": "",
16+
"remote_addr": "",
17+
"status": "accepted",
18+
"tag_ids": [],
19+
"tenant_id": "00000000-0000-4000-0000-000000000000",
20+
"uid": "5300530e3ca2f637636b4d025d2235269014865db5204b6d115386cbee89809f"
21+
},
22+
"656f60dbda27dad292686e35": {
23+
"created_at": "2023-01-05T12:00:00.000Z",
24+
"removed_at": "2023-01-06T12:00:00.000Z",
25+
"last_seen": "2023-01-05T12:00:00.000Z",
26+
"disconnected_at": null,
27+
"status_updated_at": "2023-01-06T12:00:00.000Z",
28+
"identity": {
29+
"mac": "mac-removed"
30+
},
31+
"info": null,
32+
"name": "device-removed",
33+
"position": null,
34+
"public_key": "",
35+
"remote_addr": "",
36+
"status": "removed",
37+
"tag_ids": [],
38+
"tenant_id": "00000000-0000-4000-0000-000000000000",
39+
"uid": "6600660e3ca2f637636b4d025d2235269014865db5204b6d115386cbee89809a"
40+
}
41+
}
42+
}

api/store/mongo/store_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
const (
2727
fixtureAPIKeys = "api-key" // Check "store.mongo.fixtures.api-keys" for fixture info
2828
fixtureDevices = "devices" // Check "store.mongo.fixtures.devices" for fixture info
29+
fixtureDevicesWithRemoved = "devices_with_removed" // Check "store.mongo.fixtures.devices_with_removed" for fixture info
2930
fixtureSessions = "sessions" // Check "store.mongo.fixtures.sessions" for fixture info
3031
fixtureActiveSessions = "active_sessions" // Check "store.mongo.fixtures.active_sessions" for fixture info
3132
fixtureFirewallRules = "firewall_rules" // Check "store.mongo.fixtures.firewall_rules" for fixture info

0 commit comments

Comments
 (0)