Skip to content

Commit

Permalink
opt out api status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor committed Jun 23, 2015
1 parent 9bdc77a commit 5ff9b22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
9 changes: 8 additions & 1 deletion opt_out_http_api/api_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def delete_opt_out(self, addresstype, address):
self._optouts.remove(opt_out)
return opt_out

def count_opt_outs(self):
return len(self._optouts)

def response(self, request, status_code=200, status_reason="OK", **data):
request.setResponseCode(status_code)
request.setHeader('Content-Type', 'application/json')
Expand Down Expand Up @@ -86,7 +89,6 @@ def opt_out_not_deleted(self, request, failure):
request, status_code=404,
status_reason="There\'s nothing to delete.")


# Methods

@app.route('/')
Expand Down Expand Up @@ -118,3 +120,8 @@ def delete_address(self, request, addresstype, address):
if opt_out is None:
raise OptOutNotDeleted()
return self.response(request, opt_out=opt_out)

@app.route('/optouts/count', methods=['GET'])
def count_address(self, request):
count = self.count_opt_outs()
return self.response(request, opt_out_count=count)
38 changes: 36 additions & 2 deletions opt_out_http_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def tearDown(self):

@inlineCallbacks
def start_server(self):
app = API()
self.server = yield reactor.listenTCP(0, Site(app.app.resource()))
self.app = API()
self.server = yield reactor.listenTCP(0, Site(self.app.app.resource()))
addr = self.server.getHost()
self.url = "http://%s:%s" % (addr.host, addr.port)

Expand All @@ -35,6 +35,13 @@ def api_put(self, path):

def api_delete(self, path):
return treq.delete("%s%s" % (self.url, path), persistent=False)

def api_count(self, path):
return treq.get("%s%s" % (self.url, path), persistent=False)

def add_opt_out(self, address_type, address):
self.app.save_opt_out(address_type, address)

# Tests

@inlineCallbacks
Expand Down Expand Up @@ -126,3 +133,30 @@ def test_opt_out_nothing_to_delete(self):
"reason": "There\'s nothing to delete."
},
})

@inlineCallbacks
def test_opt_out_count_two_opt_outs(self):
resp = yield self.api_count("/optouts/count")
self.assertEqual(resp.code, 200)
data = yield resp.json()
self.assertEqual(data, {
"opt_out_count": 2,
"status": {
"code": 200,
"reason": "OK"
},
})

@inlineCallbacks
def test_opt_out_count_three_opt_outs(self):
self.add_opt_out("msisdn", "+271345")
resp = yield self.api_count("/optouts/count")
self.assertEqual(resp.code, 200)
data = yield resp.json()
self.assertEqual(data, {
"opt_out_count": 3,
"status": {
"code": 200,
"reason": "OK"
},
})

0 comments on commit 5ff9b22

Please sign in to comment.