# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, 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.
from __future__ import unicode_literals

from designatedashboard.tests import base

DOMAIN_ID = '123'


class ARecordFormTests(base.BaseRecordFormCleanTests):

    IPV4 = '1.1.1.1'

    MSG_INVALID_IPV4 = 'Enter a valid IPv4 address'

    def setUp(self):
        super(ARecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'A'
        self.form.cleaned_data['name'] = self.HOSTNAME
        self.form.cleaned_data['data'] = self.IPV4

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_valid_name_field_wild_card(self):
        self.form.cleaned_data['name'] = '*'
        self.form.clean()
        self.assert_no_errors()

    def test_missing_name_field(self):
        self.form.cleaned_data['name'] = ''
        self.form.clean()
        self.assert_no_errors()
        self.assertIsNotNone(self.form.cleaned_data['name'])

    def test_missing_data_field(self):
        self.form.cleaned_data['data'] = ''
        self.form.clean()
        self.assert_required_error('data')

    def test_invalid_name_field(self):
        self.form.cleaned_data['name'] = '$#%foo!!'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_starting_dash(self):
        self.form.cleaned_data['name'] = '-ww'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_trailing_dash(self):
        self.form.cleaned_data['name'] = 'co-'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_bad_wild_card(self):
        self.form.cleaned_data['name'] = 'derp.*.' + self.DOMAIN_NAME
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_outside_of_domain_name_field(self):
        self.form.cleaned_data['name'] = 'www.bar.com.'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_data_field(self):
        self.form.cleaned_data['data'] = 'foo'
        self.form.clean()
        self.assert_error('data', self.MSG_INVALID_IPV4)


class AAAARecordFormTests(base.BaseRecordFormCleanTests):

    IPV6 = '1111:1111:1111:11::1'

    MSG_INVALID_IPV6 = 'Enter a valid IPv6 address'

    def setUp(self):
        super(AAAARecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'AAAA'
        self.form.cleaned_data['name'] = self.HOSTNAME
        self.form.cleaned_data['data'] = self.IPV6

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_valid_name_field_wild_card(self):
        self.form.cleaned_data['name'] = '*'
        self.form.clean()
        self.assert_no_errors()

    def test_missing_name_field(self):
        self.form.cleaned_data['name'] = ''
        self.form.clean()
        self.assert_no_errors()
        self.assertIsNotNone(self.form.cleaned_data['name'])

    def test_missing_data_field(self):
        self.form.cleaned_data['data'] = ''
        self.form.clean()
        self.assert_required_error('data')

    def test_invalid_name_field(self):
        self.form.cleaned_data['name'] = '#@$foo!!'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_starting_dash(self):
        self.form.cleaned_data['name'] = '-ww.foo.com'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_trailing_dash(self):
        self.form.cleaned_data['name'] = 'www.foo.co-'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_bad_wild_card(self):
        self.form.cleaned_data['name'] = 'derp.*.' + self.DOMAIN_NAME
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_outside_of_domain_name_field(self):
        self.form.cleaned_data['name'] = 'www.bar.com.'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_data_field(self):
        self.form.cleaned_data['data'] = 'foo'
        self.form.clean()
        self.assert_error('data', self.MSG_INVALID_IPV6)


class CNAMERecordFormTests(base.BaseRecordFormCleanTests):

    CNAME = 'bar.foo.com.'

    def setUp(self):
        super(CNAMERecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'CNAME'
        self.form.cleaned_data['name'] = self.HOSTNAME
        self.form.cleaned_data['data'] = self.CNAME

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_valid_name_field_wild_card(self):
        self.form.cleaned_data['name'] = '*'
        self.form.clean()
        self.assert_no_errors()

    def test_missing_name_field(self):
        self.form.cleaned_data['name'] = ''
        self.form.clean()
        self.assert_required_error('name')

    def test_missing_data_field(self):
        self.form.cleaned_data['data'] = ''
        self.form.clean()
        self.assert_required_error('data')

    def test_invalid_name_field(self):
        self.form.cleaned_data['name'] = '$#%#$foo!!!'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_starting_dash(self):
        self.form.cleaned_data['name'] = '-ww.foo.com'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_trailing_dash(self):
        self.form.cleaned_data['name'] = 'www.foo.co-'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_bad_wild_card(self):
        self.form.cleaned_data['name'] = 'derp.*.' + self.DOMAIN_NAME
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_outside_of_domain_name_field(self):
        self.form.cleaned_data['name'] = 'www.bar.com.'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_data_field(self):
        self.form.cleaned_data['data'] = 'foo'
        self.form.clean()
        self.assert_error('data', self.MSG_INVALID_HOSTNAME_SHORT)


class MXRecordFormTests(base.BaseRecordFormCleanTests):

    MAIL_SERVER = 'mail.foo.com.'
    PRIORITY = 10

    def setUp(self):
        super(MXRecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'MX'
        self.form.cleaned_data['data'] = self.MAIL_SERVER
        self.form.cleaned_data['priority'] = self.PRIORITY

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_missing_data_field(self):
        self.form.cleaned_data['data'] = ''
        self.form.clean()
        self.assert_required_error('data')

    def test_missing_priority_field(self):
        self.form.cleaned_data['priority'] = None
        self.form.clean()
        self.assert_required_error('priority')

    def test_invalid_data_field(self):
        self.form.cleaned_data['data'] = 'foo'
        self.form.clean()
        self.assert_error('data', self.MSG_INVALID_HOSTNAME_SHORT)

    def test_default_assignment_name_field(self):
        self.form.clean()
        self.assertEqual(self.DOMAIN_NAME, self.form.cleaned_data['name'])


class TXTRecordFormTests(base.BaseRecordFormCleanTests):

    TEXT = 'Lorem ipsum'

    def setUp(self):
        super(TXTRecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'TXT'
        self.form.cleaned_data['name'] = self.HOSTNAME
        self.form.cleaned_data['txt'] = self.TEXT

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_valid_name_field_wild_card(self):
        self.form.cleaned_data['name'] = '*'
        self.form.clean()
        self.assert_no_errors()

    def test_missing_name_field(self):
        self.form.cleaned_data['name'] = ''
        self.form.clean()
        self.assert_no_errors()
        self.assertIsNotNone(self.form.cleaned_data['name'])

    def test_missing_txt_field(self):
        self.form.cleaned_data['txt'] = ''
        self.form.clean()
        self.assert_required_error('txt')

    def test_invalid_name_field(self):
        self.form.cleaned_data['name'] = 'foo-'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_starting_dash(self):
        self.form.cleaned_data['name'] = '-ww.foo.com'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_trailing_dash(self):
        self.form.cleaned_data['name'] = 'www.foo.co-'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_invalid_name_field_bad_wild_card(self):
        self.form.cleaned_data['name'] = 'derp.*'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_outside_of_domain_name_field(self):
        self.form.cleaned_data['name'] = 'www.bar.com.'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_HOSTNAME)

    def test_default_assignment_data_field(self):
        self.form.clean()
        self.assertEqual(self.TEXT, self.form.cleaned_data['data'])


class SRVRecordFormTests(base.BaseRecordFormCleanTests):

    SRV_NAME = '_foo._tcp.'
    SRV_DATA = '1 1 srv.foo.com.'
    PRIORITY = 10

    MSG_INVALID_SRV_NAME = 'Enter a valid SRV name'
    MSG_INVALID_SRV_DATA = 'Enter a valid SRV record'

    def setUp(self):
        super(SRVRecordFormTests, self).setUp()
        self.form.cleaned_data['type'] = 'SRV'
        self.form.cleaned_data['name'] = self.SRV_NAME
        self.form.cleaned_data['data'] = self.SRV_DATA
        self.form.cleaned_data['priority'] = self.PRIORITY

    def test_valid_field_values(self):
        self.form.clean()
        self.assert_no_errors()

    def test_missing_name_field(self):
        self.form.cleaned_data['name'] = ''
        self.form.clean()
        self.assert_required_error('name')

    def test_missing_data_field(self):
        self.form.cleaned_data['data'] = ''
        self.form.clean()
        self.assert_required_error('data')

    def test_missing_priority_field(self):
        self.form.cleaned_data['priority'] = None
        self.form.clean()
        self.assert_required_error('priority')

    def test_invalid_name_field(self):
        self.form.cleaned_data['name'] = 'foo'
        self.form.clean()
        self.assert_error('name', self.MSG_INVALID_SRV_NAME)

    def test_invalid_data_field(self):
        self.form.cleaned_data['data'] = 'foo'
        self.form.clean()
        self.assert_error('data', self.MSG_INVALID_SRV_DATA)

    def test_default_assignment_name_field(self):
        self.form.clean()
        self.assertEqual(self.SRV_NAME + self.DOMAIN_NAME,
                         self.form.cleaned_data['name'])
