<commit_before># coding=utf-8
"""
   Copyright 2015 <NAME>

   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.

:Author:
    <NAME> (<EMAIL>)
"""
__author__ = '<EMAIL>'


import sys, os
import unittest, mock, kiss

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from sqlite3 import connect, PARSE_DECLTYPES
from datetime import date

from twisted.python import log
from twisted.cred import credentials
from twisted.cred.error import UnauthorizedLogin

from ampauth.testing import DjangoAuthChecker


class CredentialsChecker(unittest.TestCase):

    """
    Testing the server credentials handling
    """

    def _setUp_databases(self):

        self.username = 'xabi.crespo'
        self.password = '<PASSWORD>'
        self.email = '<EMAIL>'

        """
        Test database.
        """

        column = ['id', 'username', 'first_name', 'last_name', 'email',\
         'password', 'groups', 'user_permissions', 'is_staff', 'is_active',\
          'is_superuser', 'last_login', 'date_joined']

        _type = 'INTEGER'
        _typetext = 'INTEGER'
        _datetimetype = 'TEXT'

        connection = connect('test.db', detect_types = PARSE_DECLTYPES)

        connection.execute('CREATE TABLE {tn} ({nf} {ft}) '\
                            .format(tn='auth_user', nf=column[0], ft=_type))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[1], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[2], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[3], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[4], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[5], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[6], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[7], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[8], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[9], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[10], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[11], ct=_typetext))

        connection.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                            .format(tn='auth_user', cn=column[12], ct=_datetimetype))

        self.connection = connection

    def setUp(self):
        log.startLogging(sys.stdout)

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Setting database")
        self._setUp_databases()

        return


    """
    Log in with valid credentials. The server should return True
    """
    def test_GoodCredentials(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running GoodCrendentials test")

        """
        Mock object.
        """
        mockUserGoodCredentials = mock.Mock()
        mockUserGoodCredentials.username = 'xabi.crespo'
        mockUserGoodCredentials.password = '<PASSWORD>'

        creds = credentials.UsernamePassword(self.username, self.password)
        checker = DjangoAuthChecker()
        d = checker.requestAvatarId(creds, mockUserGoodCredentials)

        def checkRequestAvatarCb(result):
            self.assertEqual(result.username, self.username)

        d.addCallback(checkRequestAvatarCb)
        return d

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GoodCrendentials test ok!")


    """
    Log in with wrong username. The server should raise UnauthorizedLogin
    with 'Incorrect username' message.
    """
    def test_BadUsername(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running BadUsername test")

        """
        Mock object.
        """
        mockUserBadUsername = mock.Mock()
        mockUserBadUsername.username = 'wrongUser'
        mockUserBadUsername.password = '<PASSWORD>'

        creds = credentials.UsernamePassword(self.username, self.password)
        checker = DjangoAuthChecker()

        return self.assertRaisesRegexp(UnauthorizedLogin, 'Incorrect username',\
        checker.requestAvatarId, creds, mockUserBadUsername)

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BadUsername test ok!")


    """
    Log in with wrong password. The server should raise UnauthorizedLogin
    with 'Incorrect password' message.
    """
    def test_BadPassword(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running BadPassword test")

        """
        Mock object.
        """
        mockUserBadPassword = mock.Mock()
        mockUserBadPassword.username = 'xabi.crespo'
        mockUserBadPassword.password = '<PASSWORD>'

        creds = credentials.UsernamePassword(self.username, self.password)
        checker = DjangoAuthChecker()
        d = checker.requestAvatarId(creds, mockUserBadPassword)

        return self.assertRaisesRegexp(d, 'Incorrect password')

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BadPassword test ok!")


    """
    Erase the database file after each test.
    """
    def tearDown(self):

        try:
            self.connection.close()
            os.remove('test.db')
            log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Deleting database.")
        except:
            pass 


if __name__ == '__main__':

    unittest.main()       <commit_msg>New version with rpc calls<commit_after># coding=utf-8
"""
   Copyright 2015 <NAME>

   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.

:Author:
    <NAME> (<EMAIL>)
"""
__author__ = '<EMAIL>'


import sys, os
import unittest, mock

from unittest import TestCase

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from twisted.python import log

from ampauth.errors import BadCredentials
from rpcrequests import Satnet_RPC

class CredentialsChecker(unittest.TestCase):

    def _setUp_databases(self):

        self.username = 'xabi.crespo'
        self.password = '<PASSWORD>'
        self.email = '<EMAIL>'

    def setUp(self):
        log.startLogging(sys.stdout)

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Setting username")
        self._setUp_databases()

        return True

    """
    Log in with valid credentials. The server should return True
    """
    def test_GoodCredentials(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running GoodCrendentials test")

        """
        Mock object.
        """
        mockUserGoodCredentials = mock.Mock()
        mockUserGoodCredentials.username = 'xabi.crespo'
        mockUserGoodCredentials.password = '<PASSWORD>'

        @mock.patch('__main__.Satnet_RPC')
        def Satnet_RPC(self, sUsername, sPassword, debug=True):
            return True

        self.rpc = Satnet_RPC('xabi.crespo', mockUserGoodCredentials.password,\
         debug=True)

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GoodCrendentials test ok!")

    """
    Log in with wrong username. The server should raise BadCredentials
    with 'Incorrect username' message.
    """
    def test_BadUsername(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running BadUsername test")

        """
        Mock object.
        """
        mockUserBadUsername = mock.Mock()
        mockUserBadUsername.username = 'wrongUser'
        mockUserBadUsername.password = '<PASSWORD>'

        @mock.patch('__main__.Satnet_RPC')
        def Satnet_RPC(self, sUsername, sPassword, debug=True):
            raise BadCredentials("Incorrect username")    

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BadUsername test ok!")

        return self.assertRaisesRegexp(BadCredentials, 'Incorrect username',\
         Satnet_RPC, mockUserBadUsername.username, mockUserBadUsername.password,\
          debug=True)

    """
    Log in with wrong password. The server should raise BadCredentials
    with 'Incorrect password' message.
    """
    def test_BadPassword(self):

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running BadPassword test")

        """
        Mock object.
        """
        mockUserBadPassword = mock.Mock()
        mockUserBadPassword.username = 'xabi.crespo'
        mockUserBadPassword.password = '<PASSWORD>'

        @mock.patch('__main__.Satnet_RPC')
        def Satnet_RPC(self, sUsername, sPassword, debug=True):
            raise BadCredentials("Incorrect password")

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BadPassword test ok!")

        return self.assertRaisesRegexp(BadCredentials, 'Incorrect password',\
         Satnet_RPC, mockUserBadPassword.username, mockUserBadPassword.password,\
          debug=True)


if __name__ == '__main__':

    unittest.main()       
{'fraction_non_alphanumeric': 0.15675156174915905, 'fraction_numerical': 0.003363767419509851, 'mean_word_length': 2.7445124145376036, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 531, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '4697509', 'n_tokens_mistral': 3133, 'n_tokens_neox': 2948, 'n_words': 728}