def test_vote_failed(self): mock_get = mock.Mock(return_value=mock.Mock(text="fail")) with mock.patch.object(votes, "hn_get", mock_get) as hn_get: with mock.patch.object(votes, "_find_vote_link", return_value="good_vote_link") as find_vote: self.assertIsNone(votes.vote("token1", "up", "1234")) hn_get.assert_any_call("item?id=1234", cookies={"user": "******"}) hn_get.assert_any_call("good_vote_link", cookies={"user": "******"}) find_vote.assert_called_with(hn_get().text, "1234", "up")
def test_vote_failed(self): mock_get = mock.Mock(return_value=mock.Mock(text="fail")) with mock.patch.object(votes, "hn_get", mock_get) as hn_get: with mock.patch.object(votes, "_find_vote_link", return_value="good_vote_link") as find_vote: self.assertIsNone(votes.vote("token1", "up", "1234")) hn_get.assert_any_call('item?id=1234', cookies={'user': '******'}) hn_get.assert_any_call('good_vote_link', cookies={'user': '******'}) find_vote.assert_called_with(hn_get().text, "1234", "up")
def vote(): """Vote on an HN item""" try: success = votes.vote(request.form['token'], request.form['direction'], request.form['item']) except KeyError: abort(401) except exceptions.ClientError as e: resp = jsonify(error=e.message) resp.status_code = 403 return resp except exceptions.ServerError as e: resp = jsonify(error=e.message) resp.status_code = 500 return resp return jsonify(vote='Success' if success else 'Fail')