Skip to content

Commit

Permalink
handle section when assigning products via mgmt api (#156)
Browse files Browse the repository at this point in the history
* set proper section when storing products via mgmt api

CPCN-515

* test mgmt api user products on company update

CPCN-515

* update docs

* fix test
  • Loading branch information
petrjasek authored Dec 21, 2023
1 parent ea1ff6d commit c06e904
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 41 deletions.
22 changes: 12 additions & 10 deletions server/cp/mgmt_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import bson
import superdesk


def validate_product_refs(products):
def validate_product_refs(product_refs):
products_service = superdesk.get_resource_service("products")
for product_spec in products:
product = products_service.find_one(req=None, _id=product_spec["_id"])
assert product is not None and product["product_type"] == product_spec.get(
"section"
), (
f"invalid product type for product {product_spec['_id']}, should be {product['product_type']}"
if product
else f"unknown product {product_spec['_id']}"
)
product_ids = [bson.ObjectId(ref["_id"]) for ref in product_refs]
products = list(
products_service.get_from_mongo(req=None, lookup={"_id": {"$in": product_ids}})
)
products_by_id = {str(product["_id"]): product for product in products}

for ref in product_refs:
product = products_by_id.get(str(ref["_id"]))
assert product is not None
ref["section"] = product["product_type"]
1 change: 1 addition & 0 deletions server/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r requirements.txt

black>=23,<24
flake8
sphinx
sphinx-autobuild
Expand Down
55 changes: 44 additions & 11 deletions server/features/mgmt_api_companies.feature
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,53 @@ Feature: Management API - Companies
"contact_name": "zzz company",
"contact_email": "[email protected]",
"phone": "99999999",
"sections": {"wire": true, "agenda": true},
"products": [
{"_id": "#products._id#", "section": "wire"}
]
}
"""
Then we get error 400
Then we get response code 201

When we get "/companies/#companies._id#"
Then we get existing resource
"""
{"code": 400, "message": "invalid product type for product #products._id#, should be agenda"}
{
"products": [
{"_id": "#products._id#", "section": "agenda", "seats": 0}
]
}
"""

When we post to "/companies"
When we post to "/users"
"""
{
"name": "zzz company",
"contact_name": "zzz company",
"contact_email": "[email protected]",
"phone": "99999999",
"first_name": "John",
"last_name": "Cena",
"email": "[email protected]",
"company": "#companies._id#",
"sections": {
"wire": true,
"agenda": true
},
"products": [
{"_id": "#products._id#", "section": "agenda"}
{"_id": "#products._id#", "section": "wire"}
]
}
"""
Then we get response code 201

When we get "/users/#users._id#"
Then we get existing resource
"""
{
"products": [
{"_id": "#products._id#", "section": "agenda"}
]
}
"""


When we patch "/companies/#companies._id#"
"""
{
Expand All @@ -129,14 +152,24 @@ Feature: Management API - Companies
]
}
"""
Then we get error 400
Then we get response code 200

When we patch "/companies/#companies._id#"
When we get "/companies/#companies._id#"
Then we get existing resource
"""
{
"products": [
{"_id": "#products._id#", "section": "agenda", "seats": 0}
]
}
"""

When we get "/users/#users._id#"
Then we get existing resource
"""
{
"products": [
{"_id": "#products._id#", "section": "agenda"}
]
}
"""
Then we get response code 200
24 changes: 7 additions & 17 deletions server/features/mgmt_api_users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,17 @@ Feature: Management API - Users
]
}
"""
Then we get error 400
"""
{"code": 400, "message": "invalid product type for product #products._id#, should be agenda"}
"""
Then we get response code 201

When we post to "/users"
When we get "/users/#users._id#"
Then we get existing resource
"""
{
"first_name": "John",
"last_name": "Cena",
"email": "[email protected]",
"company": "#companies._id#",
"sections": {
"agenda": true
},
"products": [
{"section": "agenda", "_id": "#products._id#"}
{"_id": "#products._id#", "section": "agenda"}
]
}
"""
Then we get response code 201

When we patch "/users/#users._id#"
"""
Expand All @@ -170,17 +160,17 @@ Feature: Management API - Users
]
}
"""
Then we get error 400
Then we get response code 200

When we patch "/users/#users._id#"
When we get "/users/#users._id#"
Then we get existing resource
"""
{
"products": [
{"section": "agenda", "_id": "#products._id#"}
]
}
"""
Then we get response code 200

Scenario: Validate locale

Expand Down
2 changes: 1 addition & 1 deletion server/requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-auth==2.6.0
git+https://github.com/superdesk/newsroom-core.git@v2.5-rc8#egg=Newsroom-Core
git+https://github.com/superdesk/newsroom-core.git@release/2.5#egg=Newsroom-Core
2 changes: 1 addition & 1 deletion server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mongolock==1.3.4
# via superdesk-core
natsort==8.4.0
# via croniter
newsroom-core @ git+https://github.com/superdesk/newsroom-core.git@v2.5-rc8
newsroom-core @ git+https://github.com/superdesk/newsroom-core.git@release/2.5
# via -r requirements.in
oauth2client==4.1.3
# via flask-oidc-ex
Expand Down
1 change: 0 additions & 1 deletion server/theme/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ components:
$ref: '#/components/schemas/ObjectId'
required:
- _id
- section

required:
- first_name
Expand Down

0 comments on commit c06e904

Please sign in to comment.