@@ -24,6 +24,55 @@ module PlaceOS::Api
2424 end
2525 end
2626
27+ describe " authority ownership" do
28+ it " sets the authority_id from the current authority on create" do
29+ body = Model ::Generator .asset_category.to_json
30+ result = client.post(
31+ AssetCategories .base_route,
32+ body: body,
33+ headers: Spec ::Authentication .headers(sys_admin: true , support: false ),
34+ )
35+ result.status_code.should eq 201
36+
37+ created = Model ::AssetCategory .from_trusted_json(result.body)
38+ authority = Model ::Authority .find_by_domain(" localhost" ).not_nil!
39+ created.authority_id.should eq authority.id
40+ created.destroy
41+ end
42+
43+ it " rejects updates to a category owned by another authority" do
44+ other_authority = Model ::Generator .authority(domain: " https://other-asset-cat.example.com" ).save!
45+ asset_category = Model ::Generator .asset_category(other_authority).save!
46+
47+ result = client.patch(
48+ path: " #{ AssetCategories .base_route} #{ asset_category.id } " ,
49+ body: {name: " renamed-#{ random_name } " }.to_json,
50+ headers: Spec ::Authentication .headers(sys_admin: true , support: false ),
51+ )
52+ result.status_code.should eq 403
53+
54+ asset_category.destroy
55+ other_authority.destroy
56+ end
57+
58+ it " adopts a legacy category with no authority on update" do
59+ asset_category = Model ::Generator .asset_category.save!
60+ # simulate a legacy record predating the authority_id column
61+ asset_category.update_fields(authority_id: nil )
62+
63+ result = client.patch(
64+ path: " #{ AssetCategories .base_route} #{ asset_category.id } " ,
65+ body: {name: " renamed-#{ random_name } " }.to_json,
66+ headers: Spec ::Authentication .headers(sys_admin: true , support: false ),
67+ )
68+ result.success?.should be_true
69+
70+ authority = Model ::Authority .find_by_domain(" localhost" ).not_nil!
71+ Model ::AssetCategory .find!(asset_category.id).authority_id.should eq authority.id
72+ asset_category.destroy
73+ end
74+ end
75+
2776 describe " scopes" do
2877 Spec .test_controller_scope(AssetCategories )
2978 end
0 commit comments