<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# tests
# Copyright (c) 2013 <NAME> (povieira)
# See README.md and LICENSE.md for details.

import unittest
import os
import re

from noipy import authinfo
from noipy import dnsupdater
from noipy import noipy

class NoipyTest(unittest.TestCase):

    def setUp(self):
        self.test_dir = os.path.join(os.path.expanduser('~'), 'noipy_test')

    def tearDown(self):
        if os.path.exists(self.test_dir):
            os.remove(os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN))
            if not os.path.isdir(self.test_dir):
                os.remove(self.test_dir)
            else:
                os.rmdir(self.test_dir)


    def XXX_testGetIP(self):
        ip = noipy.get_ip()
        VALID_IP_REGEX = r'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'

        self.assertTrue(re.match(VALID_IP_REGEX, ip), 'get_ip() failed.')

    def testAuthObject(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        auth2 = authinfo.ApiAuth.get_instance(b'dXNlcm5hbWU6cGFzc3dvcmQ=')

        self.assertEqual(auth1, auth2, 'ApiAuth.get_instance fail.')

    def testGetAuthInstance(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        auth2 = authinfo.ApiAuth.get_instance(b'dXNlcm5hbWU6cGFzc3dvcmQ=')

        self.assertEqual(auth1, auth2, 'ApiAuth.get_instance fail.')

    def testStoreAuthInfo(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)
        auth2 = None
        try:
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file) as f:
                auth_key = f.read()
                auth2 = authinfo.ApiAuth.get_instance(auth_key.encode('utf-8'))
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        self.assertTrue(auth1 == auth2, 'Auth information storing failed.')

    def testLoadAuthInfo(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        try:
            os.mkdir(self.test_dir)
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file, 'w') as f:
                f.write(auth1.base64key.decode('utf-8'))
        except IOError as e:
            self.fail('Load settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        auth2 = authinfo.load(dnsupdater.DEFAULT_PLUGIN, self.test_dir)

        self.assertTrue(auth1 == auth2, 'Auth information loading failed.')

    def testStoreAuthInfoAfterUpgradeVersion(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        
        # ealier version settings file
        try:
            with open(self.test_dir, 'w') as f:
                f.write('test')
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)

        auth2 = None
        try:
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file) as f:
                auth_key = f.read()
                auth2 = authinfo.ApiAuth.get_instance(auth_key.encode('utf-8'))
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        self.assertTrue(auth1 == auth2, 'Auth information storing failed.')

    def testProviderExists(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)
        
        if not authinfo.exists(dnsupdater.DEFAULT_PLUGIN):
            self.fail('Settings file should be avaliable.')

        if authinfo.exists('fake_provider'):
            self.fail('Temp settings file should NOT be avaliable.')

    def testNoipPlugin(self):
        auth = authinfo.ApiAuth('username', 'password')
        hotsname = 'noipy.no-ip.org'

        plugin = dnsupdater.NoipDnsUpdater(auth, hotsname)
        plugin.update_dns('1.1.1.1')

        self.assertTrue(plugin.last_status_code in ['badauth', '401', '404'], 'Status code should be "badauth"')

    def testDynDnsPlugin(self):
        auth = authinfo.ApiAuth('username', 'password')
        hotsname = 'noipy.homelinux.com'

        plugin = dnsupdater.NoipDnsUpdater(auth, hotsname)
        plugin.update_dns('1.1.1.1')

        self.assertTrue(plugin.last_status_code in ['badauth', '401', '404'], 'Status code should be "badauth"')

if __name__ == "__main__":
    unittest.main()<commit_msg>test fix<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# tests
# Copyright (c) 2013 <NAME> (povieira)
# See README.md and LICENSE.md for details.

import unittest
import os
import re

from noipy import authinfo
from noipy import dnsupdater
from noipy import noipy

class NoipyTest(unittest.TestCase):

    def setUp(self):
        self.test_dir = os.path.join(os.path.expanduser('~'), 'noipy_test')

    def tearDown(self):
        if os.path.exists(self.test_dir):
            os.remove(os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN))
            if not os.path.isdir(self.test_dir):
                os.remove(self.test_dir)
            else:
                os.rmdir(self.test_dir)


    def testGetIP(self):
        ip = noipy.get_ip()
        VALID_IP_REGEX = r'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'

        self.assertTrue(re.match(VALID_IP_REGEX, ip), 'get_ip() failed.')

    def testAuthObject(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        auth2 = authinfo.ApiAuth.get_instance(b'dXNlcm5hbWU6cGFzc3dvcmQ=')

        self.assertEqual(auth1, auth2, 'ApiAuth.get_instance fail.')

    def testGetAuthInstance(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        auth2 = authinfo.ApiAuth.get_instance(b'dXNlcm5hbWU6cGFzc3dvcmQ=')

        self.assertEqual(auth1, auth2, 'ApiAuth.get_instance fail.')

    def testStoreAuthInfo(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)
        auth2 = None
        try:
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file) as f:
                auth_key = f.read()
                auth2 = authinfo.ApiAuth.get_instance(auth_key.encode('utf-8'))
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        self.assertTrue(auth1 == auth2, 'Auth information storing failed.')

    def testLoadAuthInfo(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        try:
            os.mkdir(self.test_dir)
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file, 'w') as f:
                f.write(auth1.base64key.decode('utf-8'))
        except IOError as e:
            self.fail('Load settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        auth2 = authinfo.load(dnsupdater.DEFAULT_PLUGIN, self.test_dir)

        self.assertTrue(auth1 == auth2, 'Auth information loading failed.')

    def testStoreAuthInfoAfterUpgradeVersion(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        
        # ealier version settings file
        try:
            with open(self.test_dir, 'w') as f:
                f.write('test')
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)

        auth2 = None
        try:
            auth_file = os.path.join(self.test_dir, dnsupdater.DEFAULT_PLUGIN)
            with open(auth_file) as f:
                auth_key = f.read()
                auth2 = authinfo.ApiAuth.get_instance(auth_key.encode('utf-8'))
        except IOError as e:
            self.fail('Store settings function failed. {0}: "{1}"'.format(e.strerror, self.test_dir))

        self.assertTrue(auth1 == auth2, 'Auth information storing failed.')

    def testProviderExists(self):
        auth1 = authinfo.ApiAuth('username', 'password')
        authinfo.store(auth1, dnsupdater.DEFAULT_PLUGIN, self.test_dir)
        
        if not authinfo.exists(dnsupdater.DEFAULT_PLUGIN, self.test_dir):
            self.fail('Settings file should be avaliable.')

        if authinfo.exists('fake_provider'):
            self.fail('Temp settings file should NOT be avaliable.')

    def testNoipPlugin(self):
        auth = authinfo.ApiAuth('username', 'password')
        hotsname = 'noipy.no-ip.org'

        plugin = dnsupdater.NoipDnsUpdater(auth, hotsname)
        plugin.update_dns('1.1.1.1')

        self.assertTrue(plugin.last_status_code in ['badauth', '401', '404'], 'Status code should be "badauth"')

    def testDynDnsPlugin(self):
        auth = authinfo.ApiAuth('username', 'password')
        hotsname = 'noipy.homelinux.com'

        plugin = dnsupdater.NoipDnsUpdater(auth, hotsname)
        plugin.update_dns('1.1.1.1')

        self.assertTrue(plugin.last_status_code in ['badauth', '401', '404'], 'Status code should be "badauth"')

if __name__ == "__main__":
    unittest.main()
{'fraction_non_alphanumeric': 0.13591415947822427, 'fraction_numerical': 0.023143277929728593, 'mean_word_length': 2.6992217898832687, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2440772', 'n_tokens_mistral': 3322, 'n_tokens_neox': 3235, 'n_words': 585}