Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
replace make_reponse for jsonify on endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Tati authored and tbanjos committed Dec 26, 2019
1 parent 9359539 commit cd958a3
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 38 deletions.
Empty file added tests/mocks/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):
def of_id(self, gang_id: int) -> Gang:
return self.gang

def add(self, new_gang: Gang) -> NoReturn:
def add(self, new_gang: Gang) -> int:
pass

def update(self, mod_gang: Gang) -> NoReturn:
Expand All @@ -38,4 +38,4 @@ def all(self) -> List[Gang]:
gang2.add_members(members_gang2)
gang3.add_members(members_gang3)

return [gang1, gang2, gang3]
return [gang1, gang2, gang3]
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def __init__(self):
def of_id(self, outlaw_id) -> Outlaw:
return self.outlaw

def add(self, new_outlaw: Outlaw) -> NoReturn:
def add(self, new_outlaw: Outlaw) -> int:
self.outlaw = new_outlaw
return 1

def update(self, mod_outlaw: Outlaw) -> NoReturn:
self.outlaw = mod_outlaw
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions tests/test_create_gang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from tests.mocks.mock_gang_repository import MockGangRepository
from thesheriff.application.gang.request.create_gang_request import CreateGangRequest
from thesheriff.application.outlaw.create_gang import CreateGang


def test_create_gang():
gang_repository = MockGangRepository()

request = CreateGangRequest(1, "super gang")

gang = CreateGang(gang_repository).execute(request)

assert gang.owner_id == 1
assert gang.name == "super gang"
2 changes: 1 addition & 1 deletion tests/test_create_outlaw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mocks.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.outlaw.create_outlaw import CreateOutlaw
from thesheriff.application.outlaw.request.create_outlaw_request import CreateOutlawRequest

Expand Down
6 changes: 3 additions & 3 deletions tests/test_create_raid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tests.mock_gang_repository import MockGangRepository
from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mock_raid_repository import MockRaidRepository
from tests.mocks.mock_gang_repository import MockGangRepository
from tests.mocks.mock_outlaw_repository import MockOutlawRepository
from tests.mocks.mock_raid_repository import MockRaidRepository
from thesheriff.application.raid.create_raid import CreateRaid
from thesheriff.application.raid.request.create_raid_request import CreateRaidRequest
from thesheriff.domain.outlaw.outlaw import Outlaw
Expand Down
2 changes: 1 addition & 1 deletion tests/test_end_raid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from tests.mock_raid_repository import MockRaidRepository
from tests.mocks.mock_raid_repository import MockRaidRepository
from thesheriff.application.raid.end_raid import EndRaid
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.outlaw.outlaw import Outlaw
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gang_list_gangs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tests.mock_gang_repository import MockGangRepository
from tests.mocks.mock_gang_repository import MockGangRepository
from thesheriff.application.gang.list_gangs import ListGangs


Expand Down
2 changes: 1 addition & 1 deletion tests/test_grade_raid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mocks.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.raid.grade_raid import GradeRaid
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.raid.raid import Raid
Expand Down
2 changes: 1 addition & 1 deletion tests/test_list_friends.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mocks.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.outlaw.list_friends import ListFriends


Expand Down
2 changes: 1 addition & 1 deletion tests/test_outlaw_list_gangs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tests.mock_gang_repository import MockGangRepository
from tests.mocks.mock_gang_repository import MockGangRepository
from thesheriff.application.outlaw.list_gangs import ListGangs


Expand Down
4 changes: 2 additions & 2 deletions tests/test_rate_raid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime

from tests.mock_raid_repository import MockRaidRepository
from tests.mock_outlaw_repository import MockOutlawRepository
from tests.mocks.mock_raid_repository import MockRaidRepository
from tests.mocks.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.outlaw.rate_raid import RateRaid
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.outlaw.sheriff import Sheriff
Expand Down
3 changes: 3 additions & 0 deletions thesheriff/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Column, DateTime, Float, ForeignKey, Integer, MetaData, Table, Text
)
from thesheriff.domain.gang.repository.gang_repository import GangRepository
from thesheriff.domain.mail.notifier.mail_notifier import MailNotifier
from thesheriff.domain.outlaw.repository.outlaw_repository import \
OutlawRepository
from thesheriff.domain.raid.repository.raid_repository import RaidRepository
Expand All @@ -15,6 +16,7 @@
MySQLOutlawRepository
from thesheriff.infrastructure.repository.mysql_raid_repository import \
MySQLRaidRepository
from thesheriff.infrastructure.mail.smtp_mail_notifier import SMTPMailNotifier

METADATA = MetaData()

Expand Down Expand Up @@ -76,5 +78,6 @@ def config(binder: inject.Binder) -> None:
application.config['DATABASE_URI'],
application.config['METADATA'],
application.config['OUTLAW_TABLE']))
binder.bind(MailNotifier, SMTPMailNotifier)

inject.configure(config)
2 changes: 1 addition & 1 deletion thesheriff/domain/gang/repository/gang_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def of_id(self, gang_id: int) -> Gang:
pass

@abc.abstractmethod
def add(self, new_gang: Gang) -> NoReturn:
def add(self, new_gang: Gang) -> int:
pass

@abc.abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion thesheriff/domain/outlaw/repository/outlaw_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def of_id(self, outlaw_id: int) -> Outlaw:
pass

@abc.abstractmethod
def add(self, new_outlaw: Outlaw) -> NoReturn:
def add(self, new_outlaw: Outlaw) -> int:
pass

@abc.abstractmethod
Expand Down
11 changes: 7 additions & 4 deletions thesheriff/infrastructure/controllers/gang_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import inject
from flask import Blueprint, jsonify, Response, request, make_response
from flask import Blueprint, jsonify, Response, request

from thesheriff.application.gang.list_gangs import ListGangs
from thesheriff.application.gang.request.create_gang_request import \
Expand Down Expand Up @@ -117,8 +117,11 @@ def create_gang_endpoint() -> Response:
new_gang.get('name'))
gang = create_gang.execute(gang_request)

gang_json = json.dumps(gang.__dict__)
resp = make_response(gang_json, 201)
return resp
result = dict({'id': gang.id, 'name': gang.name,
'owner_id': gang.owner_id})

message = {'status': 201, 'gang created': result}

return jsonify(message)

return blueprint_gang
7 changes: 5 additions & 2 deletions thesheriff/infrastructure/controllers/outlaw_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ def create_outlaw_endpoint() -> Response:
# TODO(all): if outlaw is None we should return an HTTP error
new_outlaw = data.get('outlaw')

create_outlaw.execute(CreateOutlawRequest(
outlaw = create_outlaw.execute(CreateOutlawRequest(
new_outlaw.get('name'), new_outlaw.get('email')))

message = {'status': 201, 'message': 'Outlaw added successfully'}
result = dict({'id': outlaw.id, 'name': outlaw.name,
'email': outlaw.email})

message = {'status': 201, 'outlaw created': result}

return jsonify(message)

Expand Down
19 changes: 12 additions & 7 deletions thesheriff/infrastructure/controllers/raid_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
This module implements the RESTful part of the Raid use cases.
"""
import json

import inject
from flask import Blueprint, Response, request, make_response
from flask import Blueprint, Response, request, jsonify

from thesheriff.application.outlaw.rate_raid import RateRaid
from thesheriff.application.raid.create_raid import CreateRaid
Expand Down Expand Up @@ -85,9 +84,13 @@ def create_raid() -> Response:

raid = create_raid.execute(raid_request)

raid_json = json.dumps(raid.__dict__)
resp = make_response(raid_json, 201)
return resp
result = dict({'id': raid.id, 'name': raid.name, 'date': raid.date,
'location': raid.location, 'gang_id': raid.gang.id,
'sheriff_id': raid.sheriff.id, 'outlaws': raid.outlaws})

message = {'status': 201, 'gang created': result}

return jsonify(message)

@blueprint_raid.route("/outlaw/<int:outlaw_id>/raid/<int:raid_id>/",
methods=['PUT'])
Expand All @@ -104,7 +107,9 @@ def rate_raid(outlaw_id: int, raid_id: int) -> Response:
rate = data.get('rate')
score = Score(**rate)
rate_raid.execute(outlaw_id, raid_id, score)
message = {'message': 'rated successfully'}
return make_response(message, 204)

message = {'status': 204, 'message': 'rated successfully'}

return jsonify(message)

return blueprint_raid
Empty file.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import smtplib
import ssl
from thesheriff.domain.mail.repository.mail_notification \
import MailNotification

from thesheriff.domain.mail.notifier.mail_notifier import MailNotifier

class SMTPMailRepository:

class SMTPMailNotifier(MailNotifier):
global port # For SSL
global context
global password
Expand All @@ -15,9 +15,6 @@ class SMTPMailRepository:
# TODO(all): read the password from an environment variable
password = "thesheriff123"

def __init__(self, mail_repository: MailNotification):
self.mail_repository = mail_repository

def send(self, mail):
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) \
as server:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def add(self, new_gang: Gang) -> NoReturn:
"""
query = self.__gang_table.insert()\
.values(owner_id=new_gang.owner_id, name=new_gang.name)
self.__connection.execute(query)
result = self.__connection.execute(query)
id = result.lastrowid
new_gang.id = id
return id

def update(self, mod_gang: Gang) -> NoReturn:
"""Method update modifies existing Gang.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def of_id(self, outlaw_id: int) -> Outlaw:
self.__outlaw_table.c.id == outlaw_id)
return self.__connection.execute(query)

def add(self, new_outlaw: Outlaw) -> NoReturn:
def add(self, new_outlaw: Outlaw) -> int:
"""Method add persists a new Outlaw to MySQL.
:param new_outlaw: Object with the new Outlaw details
Expand All @@ -46,7 +46,10 @@ def add(self, new_outlaw: Outlaw) -> NoReturn:
"""
query = self.__outlaw_table.insert().values(
name=new_outlaw.name, email=new_outlaw.email)
self.__connection.execute(query)
result = self.__connection.execute(query)
id = result.lastrowid
new_outlaw.id = id
return id

def update(self, mod_outlaw: Outlaw) -> NoReturn:
"""Method update modifies existing Outlaw.
Expand Down

0 comments on commit cd958a3

Please sign in to comment.