Exemplo n.º 1
0
def test_check_status_without_schedule(app):
    """Assert that the function return correct response without schedule setup."""
    with app.app_context():
        service_name = 'PAYBC'

        # without service schedule setup
        schedule_json = None
        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(
            service_name=service_name, check_date=arrow.Arrow.utcnow())
        mock_get.stop()

        assert response is not None
        assert response['service'] == service_name
        assert response['current_status'] == 'None'
        assert response['next_up_time'] == 0

        # without service outage and available schedule setup expect return no outage
        schedule_json = [{'service_name': 'PAYBC'}]
        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(
            service_name=service_name, check_date=arrow.Arrow.utcnow())
        mock_get.stop()

        assert response is not None
        assert response['service'] == service_name
        assert response['current_status'] == 'True'
        assert response['next_up_time'] == 0
Exemplo n.º 2
0
def test_check_status_with_custom(app):
    """Assert that the function returns schedules."""
    time_yday = datetime.now() - timedelta(days=1)
    time_tomorrow = time_yday + timedelta(days=2)

    time_str = arrow.get(time_yday).replace(
        tzinfo='US/Pacific').strftime('%Y-%m-%d %H:%M')
    time_tomorrow_str = arrow.get(time_tomorrow).replace(
        tzinfo='US/Pacific').strftime('%Y-%m-%d %H:%M')

    schedule_json = [{
        'custom': {
            'start': time_str,
            'end': time_tomorrow_str,
            'message': 'Test-Warning'
        }
    }]

    with app.app_context():
        service_name = 'PAYBC'
        # Test the custom message for today
        check_date: arrow.Arrow = arrow.get(datetime.now())

        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(service_name=service_name,
                                                check_date=check_date)
        mock_get.stop()

        # match outage windows
        assert response is not None
        assert response['service'] == service_name
        assert response['custom_message'] == 'Test-Warning'

        # Test the custom message for 1 day and 1 sec from now and custom_message should not be present
        time_day_after = time_tomorrow + timedelta(days=1)
        check_date: arrow.Arrow = arrow.get(time_day_after)

        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(service_name=service_name,
                                                check_date=check_date)
        mock_get.stop()

        # match outage windows
        assert response is not None
        assert response['service'] == service_name
        assert response.get('custom_message', None) is None
Exemplo n.º 3
0
def test_check_status_no_outage(app):
    """Assert that the function returns schedules."""
    schedule_json = [{
        'available': [{
            'dayofweek': '1',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '2',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '3',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '4',
            'from': '15:05',
            'to': '21:00'
        }, {
            'dayofweek': '5',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '6',
            'from': '6:30',
            'to': '21:00'
        }, {
            'dayofweek': '7',
            'from': '6:30',
            'to': '21:00'
        }],
        'outage': [{
            'start': '2019-10-23 10:00',
            'end': '2019-10-23 18:00'
        }, {
            'start': '2019-10-24 13:00',
            'end': '2019-10-25 15:05'
        }]
    }]

    with app.app_context():
        service_name = 'PAYBC'
        #  Localtime: Sunday, November 24, 2019 10:00:00 AM GMT-08:00
        check_date: arrow.Arrow = arrow.get('2019-11-24 18:00',
                                            'YYYY-MM-DD HH:mm')

        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(service_name=service_name,
                                                check_date=check_date)
        mock_get.stop()

        # no outage, match available schedule
        assert response is not None
        assert response['service'] == service_name
        assert response['current_status'] == 'True'
Exemplo n.º 4
0
def test_get_outage_schedule(app):
    """Assert that the get outage schedule function return correct response."""
    schedule_json = [{
        'available': [{
            'dayofweek': '1',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '2',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '3',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '4',
            'from': '15:05',
            'to': '21:00'
        }, {
            'dayofweek': '5',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '6',
            'from': '6:30',
            'to': '21:00'
        }, {
            'dayofweek': '7',
            'from': '6:30',
            'to': '21:00'
        }],
        'outage': [{
            'start': '2019-10-23 16:00',
            'end': '2019-10-23 18:00'
        }, {
            'start': '2019-10-23 16:00',
            'end': '2019-10-23 17:00'
        }, {
            'start': '2019-10-23 13:00',
            'end': '2019-10-25 15:05'
        }]
    }]

    with app.app_context():
        response = StatusService().get_outage_schedules(schedule_json)

        assert response is not None
        assert response[0][0] == arrow.get(
            '2019-10-23 16:00',
            'YYYY-MM-DD HH:mm').replace(tzinfo='US/Pacific')
        assert response[2][1] == arrow.get(
            '2019-10-25 15:05',
            'YYYY-MM-DD HH:mm').replace(tzinfo='US/Pacific')

        schedule_json = [{
            'available': [{
                'dayofweek': '1',
                'from': '6:00',
                'to': '21:00'
            }, {
                'dayofweek': '2',
                'from': '6:00',
                'to': '21:00'
            }, {
                'dayofweek': '3',
                'from': '6:00',
                'to': '21:00'
            }, {
                'dayofweek': '4',
                'from': '15:05',
                'to': '21:00'
            }, {
                'dayofweek': '5',
                'from': '6:00',
                'to': '21:00'
            }, {
                'dayofweek': '6',
                'from': '6:30',
                'to': '21:00'
            }, {
                'dayofweek': '7',
                'from': '6:30',
                'to': '21:00'
            }]
        }]

        response = StatusService().get_outage_schedules(schedule_json)

        assert response is not None
        assert len(response) == 0
Exemplo n.º 5
0
def test_check_status_with_outage(app):
    """Assert that the function returns schedules."""
    schedule_json = [{
        'available': [{
            'dayofweek': '1',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '2',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '3',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '4',
            'from': '15:05',
            'to': '21:00'
        }, {
            'dayofweek': '5',
            'from': '6:00',
            'to': '21:00'
        }, {
            'dayofweek': '6',
            'from': '6:30',
            'to': '21:00'
        }, {
            'dayofweek': '7',
            'from': '6:30',
            'to': '21:00'
        }],
        'outage': [{
            'start': '2019-10-23 10:00',
            'end': '2019-10-23 18:00'
        }, {
            'start': '2019-10-24 13:00',
            'end': '2019-10-25 15:05'
        }]
    }]

    with app.app_context():
        service_name = 'PAYBC'
        #  Localtime: Wednesday, October 23, 2019 3:00:00 PM GMT-07:00
        check_date: arrow.Arrow = arrow.get('2019-10-23 22:00',
                                            'YYYY-MM-DD HH:mm')

        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(service_name=service_name,
                                                check_date=check_date)
        mock_get.stop()

        # match outage windows
        assert response is not None
        assert response['service'] == service_name
        assert response['current_status'] == 'False'

        #  Localtime:  Friday, October 25, 2019 5:00:00 AM GMT-07:00
        check_date: arrow.Arrow = arrow.get('2019-10-25 12:00',
                                            'YYYY-MM-DD HH:mm')

        mock_get_schedule = patch(
            'status_api.services.status.Status.get_schedules')
        mock_get = mock_get_schedule.start()
        mock_get.return_value = schedule_json
        response = StatusService().check_status(service_name=service_name,
                                                check_date=check_date)
        mock_get.stop()

        # match outage windows
        assert response is not None
        assert response['service'] == service_name
        assert response['current_status'] == 'False'
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Resource for Service status endpoints."""
from http import HTTPStatus

from arrow import Arrow
from flask import current_app, jsonify
from flask_restplus import Namespace, Resource, cors

from status_api.services.status import Status as StatusService
from status_api.utils.util import cors_preflight

API = Namespace('status', description='Status Service System')

STATUS_SERVICE = StatusService()


@cors_preflight('GET')
@API.route('', methods=['GET', 'OPTIONS'])
class ServiceStatus(Resource):
    """Endpoint resource to calculate fee."""
    @staticmethod
    @cors.crossdomain(origin='*')
    @API.response(200, 'OK')
    def get(service_name: str):
        """Get the service schedule and return status and next schedule date/time."""
        current_app.logger.info('<ServiceStatus.get')
        response, status = STATUS_SERVICE.check_status(
            service_name, Arrow.utcnow()), HTTPStatus.OK
        current_app.logger.debug('>ServiceStatus.get')