<commit_before>from flask import json
from nose.tools import assert_equal, assert_in, assert_not_equal, \
    assert_almost_equal

from app import db
from app.models import Service
from datetime import datetime, timedelta
from .helpers import BaseApplicationTest, JSONUpdateTestMixin


class TestListServices(BaseApplicationTest):
    def test_list_services_with_no_services(self):
        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(data['services'], [])

    def test_list_services(self):
        self.setup_dummy_services(1)
        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 1)

    def test_paginated_list_services_page_one(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 10)
        next_link = first_by_rel('next', data['links'])
        assert_in("page=2", next_link['href'])

    def test_paginated_list_services_page_two(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services?page=2')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 5)
        prev_link = first_by_rel('prev', data['links'])
        assert_in("page=1", prev_link['href'])

    def test_paginated_list_services_page_out_of_range(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services?page=10')

        # TODO: decide whether this is actually correct.
        #       this is what Flask-SQLAlchemy does by default so is easy
        assert_equal(response.status_code, 404)

    def test_x_forwarded_proto(self):
        self.setup_dummy_services(1)

        response = self.client.get('/services',
                                   headers={'X-Forwarded-Proto': 'https'})
        data = json.loads(response.get_data())

        assert data['services'][0]['links'][0]['href'].startswith('https://')


def first_by_rel(rel, links):
    for link in links:
        if link['rel'] == rel:
            return link


class TestPostService(BaseApplicationTest):
    method = "post"
    endpoint = "/services"

    def test_post_is_not_supported(self):
        payload = self.load_example_listing("SSP-JSON-IaaS")
        response = self.client.post(<commit_msg>Add tests for invalid page and supplier_id<commit_after>from flask import json
from nose.tools import assert_equal, assert_in, assert_not_equal, \
    assert_almost_equal

from app import db
from app.models import Service
from datetime import datetime, timedelta
from .helpers import BaseApplicationTest, JSONUpdateTestMixin


class TestListServices(BaseApplicationTest):
    def test_list_services_with_no_services(self):
        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(data['services'], [])

    def test_list_services(self):
        self.setup_dummy_services(1)
        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 1)

    def test_paginated_list_services_page_one(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 10)
        next_link = first_by_rel('next', data['links'])
        assert_in("page=2", next_link['href'])

    def test_paginated_list_services_page_two(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services?page=2')
        data = json.loads(response.get_data())

        assert_equal(response.status_code, 200)
        assert_equal(len(data['services']), 5)
        prev_link = first_by_rel('prev', data['links'])
        assert_in("page=1", prev_link['href'])

    def test_paginated_list_services_page_out_of_range(self):
        self.setup_dummy_services(15)

        response = self.client.get('/services?page=10')

        # TODO: decide whether this is actually correct.
        #       this is what Flask-SQLAlchemy does by default so is easy
        assert_equal(response.status_code, 404)

    def test_x_forwarded_proto(self):
        self.setup_dummy_services(1)

        response = self.client.get('/services',
                                   headers={'X-Forwarded-Proto': 'https'})
        data = json.loads(response.get_data())

        assert data['services'][0]['links'][0]['href'].startswith('https://')

    def test_invalid_page_argument(self):
        response = self.client.get('/services?page=a')

        assert_equal(response.status_code, 400)

    def test_invalid_supplier_id_argument(self):
        response = self.client.get('/services?supplier_id=a')

        assert_equal(response.status_code, 400)


def first_by_rel(rel, links):
    for link in links:
        if link['rel'] == rel:
            return link


class TestPostService(BaseApplicationTest):
    method = "post"
    endpoint = "/services"

    def test_post_is_not_supported(self):
        payload = self.load_example_listing("SSP-JSON-IaaS")
        response = self.client.post(
{'fraction_non_alphanumeric': 0.11605070523120871, 'fraction_numerical': 0.013211926441706838, 'mean_word_length': 3.2796027501909855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2821501', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1647, 'n_words': 301}