Skip to content

Commit

Permalink
Bugfix/daily notification (#1019)
Browse files Browse the repository at this point in the history
* Harding commander permission

* Fixing tests

* Fixing tests

* fixing test database user

* Removing print
  • Loading branch information
kevgliss authored Apr 12, 2021
1 parent 78c01b5 commit 7019083
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 33 deletions.
11 changes: 8 additions & 3 deletions src/dispatch/auth/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def has_required_permissions(
current_organization = organization_service.get_by_name(
db_session=request.state.db, name=request.path_params["organization"]
)

if not current_organization:
return

current_user = get_current_user(db_session=request.state.db, request=request)

for org in current_user.organizations:
Expand Down Expand Up @@ -258,7 +262,8 @@ def has_required_permissions(
db_session=request.state.db, incident_id=request.path_params["incident_id"]
)
if not current_incident:
return False
return

if current_incident.commander.individual.email == current_user.email:
return True
if current_incident.commander:
if current_incident.commander.individual.email == current_user.email:
return True
5 changes: 4 additions & 1 deletion src/dispatch/document/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
INCIDENT_RESOURCE_INVESTIGATION_SHEET_TEMPLATE,
INCIDENT_RESOURCE_INCIDENT_FAQ_DOCUMENT,
)
from dispatch.project import service as project_service
from dispatch.incident_priority import service as incident_priority_service
from dispatch.incident_type import service as incident_type_service
from dispatch.term import service as term_service
Expand Down Expand Up @@ -112,16 +113,18 @@ def create(*, db_session, document_in: DocumentCreate) -> Document:
incident_type_service.get_by_name(db_session=db_session, name=n.name)
for n in document_in.incident_types
]
project = project_service.get_by_name(db_session=db_session, name=document_in.project.name)

# set the last reminder to now
if document_in.evergreen:
document_in.evergreen_last_reminder_at = datetime.utcnow()

document = Document(
**document_in.dict(exclude={"terms", "incident_priorities", "incident_types"}),
**document_in.dict(exclude={"terms", "incident_priorities", "incident_types", "project"}),
incident_priorities=incident_priorities,
incident_types=incident_types,
terms=terms,
project=project,
)

db_session.add(document)
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/incident/scheduled.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def daily_report(db_session=None):

notification_service.send(
db_session=db_session,
project_id=project_id,
project_id=notification.project.id,
notification=notification,
notification_params=notification_params,
)
Expand Down
11 changes: 3 additions & 8 deletions src/dispatch/incident_cost/scheduled.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from dispatch.incident import service as incident_service
from dispatch.decorators import background_task
from dispatch.project.models import ProjectRead
from dispatch.incident_cost.models import IncidentCostCreate
from dispatch.incident_cost_type.models import IncidentCostTypeCreate
from dispatch.incident_cost_type import service as incident_cost_type_service
from dispatch.incident_cost_type.models import IncidentCostTypeRead
from dispatch.scheduler import scheduler

from .service import (
Expand Down Expand Up @@ -46,12 +45,8 @@ def calculate_incidents_response_cost(db_session=None):

if not incident_response_cost:
# we create the response cost if it doesn't exist
incident_cost_in = IncidentCostCreate(
incident_cost_type=IncidentCostTypeRead(
**response_cost_type.__dict__,
),
project=ProjectRead(**incident.project.__dict__),
)
incident_cost_type = IncidentCostTypeCreate.from_orm(response_cost_type)
incident_cost_in = IncidentCostCreate(incident_cost_type=incident_cost_type)
incident_response_cost = create(
db_session=db_session, incident_cost_in=incident_cost_in
)
Expand Down
8 changes: 7 additions & 1 deletion src/dispatch/incident_priority/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi.encoders import jsonable_encoder
from sqlalchemy.sql.expression import true

from dispatch.project import service as project_service
from .models import IncidentPriority, IncidentPriorityCreate, IncidentPriorityUpdate


Expand Down Expand Up @@ -39,7 +40,12 @@ def get_all(*, db_session) -> List[Optional[IncidentPriority]]:

def create(*, db_session, incident_priority_in: IncidentPriorityCreate) -> IncidentPriority:
"""Creates an incident priority."""
incident_priority = IncidentPriority(**incident_priority_in.dict())
project = project_service.get_by_name(
db_session=db_session, name=incident_priority_in.project.name
)
incident_priority = IncidentPriority(
**incident_priority_in.dict(exclude={"project"}), project=project
)
db_session.add(incident_priority)
db_session.commit()
return incident_priority
Expand Down
5 changes: 4 additions & 1 deletion src/dispatch/incident_type/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi.encoders import jsonable_encoder
from sqlalchemy.sql.expression import true

from dispatch.project import service as project_service
from dispatch.document import service as document_service
from dispatch.service import service as service_service

Expand Down Expand Up @@ -36,8 +37,10 @@ def get_all(*, db_session) -> List[Optional[IncidentType]]:

def create(*, db_session, incident_type_in: IncidentTypeCreate) -> IncidentType:
"""Creates an incident type."""
project = project_service.get_by_name(db_session=db_session, name=incident_type_in.project.name)
incident_type = IncidentType(
**incident_type_in.dict(exclude={"commander_service", "template_document"}),
**incident_type_in.dict(exclude={"commander_service", "template_document", "project"}),
project=project,
)

if incident_type_in.template_document:
Expand Down
9 changes: 8 additions & 1 deletion src/dispatch/individual/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dispatch.database.core import SessionLocal

from dispatch.incident.models import Incident
from dispatch.project import service as project_service
from dispatch.incident_priority import service as incident_priority_service
from dispatch.incident_type import service as incident_type_service
from dispatch.term import service as term_service
Expand Down Expand Up @@ -65,6 +66,9 @@ def get_or_create(

def create(*, db_session, individual_contact_in: IndividualContactCreate) -> IndividualContact:
"""Creates an individual."""
project = project_service.get_by_name(
db_session=db_session, name=individual_contact_in.project.name
)
terms = [
term_service.get_or_create(db_session=db_session, term_in=t)
for t in individual_contact_in.terms
Expand All @@ -78,10 +82,13 @@ def create(*, db_session, individual_contact_in: IndividualContactCreate) -> Ind
for n in individual_contact_in.incident_types
]
contact = IndividualContact(
**individual_contact_in.dict(exclude={"terms", "incident_priorities", "incident_types"}),
**individual_contact_in.dict(
exclude={"terms", "incident_priorities", "incident_types", "project"}
),
terms=terms,
incident_types=incident_types,
incident_priorities=incident_priorities,
project=project,
)
db_session.add(contact)
db_session.commit()
Expand Down
5 changes: 4 additions & 1 deletion src/dispatch/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi.encoders import jsonable_encoder

from dispatch.exceptions import InvalidConfiguration
from dispatch.project import service as project_service
from dispatch.incident_priority import service as incident_priority_service
from dispatch.incident_type import service as incident_type_service
from dispatch.plugin import service as plugin_service
Expand Down Expand Up @@ -45,6 +46,7 @@ def get_all_by_type_and_status(

def create(*, db_session, service_in: ServiceCreate) -> Service:
"""Creates a new service."""
project = project_service.get_by_name(db_session=db_session, name=service_in.project.name)
terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms]
incident_priorities = [
incident_priority_service.get_by_name(db_session=db_session, name=n.name)
Expand All @@ -55,10 +57,11 @@ def create(*, db_session, service_in: ServiceCreate) -> Service:
for n in service_in.incident_types
]
service = Service(
**service_in.dict(exclude={"terms", "incident_priorities", "incident_types"}),
**service_in.dict(exclude={"terms", "incident_priorities", "incident_types", "project"}),
incident_priorities=incident_priorities,
incident_types=incident_types,
terms=terms,
project=project,
)
db_session.add(service)
db_session.commit()
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/static/dispatch/src/router/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const protectedRoute = [
meta: { requiresAuth: true },
redirect: {
name: "IncidentOverview",
params: { organization: "Default" },
params: { organization: "default" },
},
},
...withPrefix("/:organization/", [
Expand Down
6 changes: 1 addition & 5 deletions src/dispatch/static/dispatch/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
features: ["find"],
},
])
config.resolve.alias.set("@$", resolve("src")).set("@views", resolve("src/views"))
},
configureWebpack: {
plugins: [
Expand All @@ -24,11 +25,6 @@ module.exports = {
],
devtool: "source-map",
},

chainWebpack: (config) => {
config.resolve.alias.set("@$", resolve("src")).set("@views", resolve("src/views"))
},

css: {
loaderOptions: {
less: {
Expand Down
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from starlette.config import environ

# set test config
environ["SECRET_PROVIDER"] = ""
environ["DATABASE_CREDENTIALS"] = "postgres:dispatch"
environ["DATABASE_HOSTNAME"] = "localhost"
environ["DISPATCH_HELP_EMAIL"] = "example@example.com"
Expand Down Expand Up @@ -81,10 +82,11 @@ def testapp():

@pytest.fixture(scope="session", autouse=True)
def db():
if database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
drop_database(str(config.SQLALCHEMY_DATABASE_URI))

create_database(str(config.SQLALCHEMY_DATABASE_URI))
try:
if database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
drop_database(str(config.SQLALCHEMY_DATABASE_URI))
except Exception:
create_database(str(config.SQLALCHEMY_DATABASE_URI))
Base.metadata.create_all(engine) # Create the tables.
_db = SessionLocal()
yield _db
Expand Down
2 changes: 1 addition & 1 deletion tests/document/test_document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_create(session, project):
resource_id=resource_id,
resource_type=resource_type,
weblink=weblink,
project={"id": project.id, "name": project.name},
project=project,
)
document = create(db_session=session, document_in=document_in)
assert document
Expand Down
2 changes: 1 addition & 1 deletion tests/incident_priority/test_incident_priority_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_create(session, project):
incident_priority_in = IncidentPriorityCreate(
name=name,
description=description,
project={"id": project.id, "name": project.name},
project=project,
)
incident_priority = create(db_session=session, incident_priority_in=incident_priority_in)
assert incident_priority
Expand Down
2 changes: 1 addition & 1 deletion tests/incident_type/test_incident_type_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_create(session, project, document):
incident_type_in = IncidentTypeCreate(
name=name,
template_document=document,
project={"id": project.id, "name": project.name},
project=project,
)

incident_type = create(db_session=session, incident_type_in=incident_type_in)
Expand Down
4 changes: 2 additions & 2 deletions tests/individual/test_individual_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_get_or_create(session, project, individual_contact):
mobile_phone=mobile_phone,
office_phone=office_phone,
weblink=weblink,
project={"id": project.id, "name": project.name},
project=project.__dict__,
)
contact = create(db_session=session, individual_contact_in=individual_contact_in)

Expand All @@ -65,7 +65,7 @@ def test_create(session, project):
mobile_phone=mobile_phone,
office_phone=office_phone,
weblink=weblink,
project={"id": project.id, "name": project.name},
project=project,
)
individual_contact = create(db_session=session, individual_contact_in=individual_contact_in)
assert individual_contact
Expand Down
2 changes: 1 addition & 1 deletion tests/service/test_service_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_create(session, project):
name = "createName"
service_in = ServiceCreate(
name=name,
project={"id": project.id, "name": project.name},
project=project,
)

service = create(db_session=session, service_in=service_in)
Expand Down

0 comments on commit 7019083

Please sign in to comment.