Unit test auth user

This commit is contained in:
Mauricio Baeza 2021-05-19 22:26:45 -05:00
parent bb4b27886e
commit 7f364677d7
4 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,3 @@
#!/usr/bin/env python3
from .server import ACloudServer

57
source/acloud/server.py Normal file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env python
# ~
# ~ Copyright (C) 2021 Mauricio Baeza Servin - elmau [AT] correlibre [DOT] net
# ~
# ~ This program is free software: you can redistribute it and/or modify
# ~ it under the terms of the GNU General Public License as published by
# ~ the Free Software Foundation, either version 3 of the License, or
# ~ (at your option) any later version.
# ~
# ~ This program is distributed in the hope that it will be useful,
# ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
# ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# ~ GNU General Public License for more details.
# ~
# ~ You should have received a copy of the GNU General Public License
# ~ along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import httpx
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
LOG_DATE = '%d/%m/%Y %H:%M:%S'
logging.addLevelName(logging.ERROR, '\033[1;41mERROR\033[1;0m')
logging.addLevelName(logging.DEBUG, '\x1b[33mDEBUG\033[1;0m')
logging.addLevelName(logging.INFO, '\x1b[32mINFO\033[1;0m')
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=LOG_DATE)
log = logging.getLogger(__name__)
TIMEOUT = 10
class ACloudServer(object):
def __init__(self, server, version='v1'):
self._server = f'{server}/api/{version}'
self._token = ''
@property
def token(self):
return self._token
@token.setter
def token(self, value):
self._token = value
def login(self, user, contra):
data = {'email': user, 'password': contra, 'token_name': 'token'}
result = httpx.post(f'{self._server}/auth/login', data=data)
if result.status_code == 422:
return False
data = result.json()
self._token = data['user']['access_token']
return result.status_code == 200

12
source/acloud_cli.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from acloud import ACloudServer
def main():
return
if __name__ == '__main__':
main()

29
source/tests/tests.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
import sys
import unittest
sys.path.append('..')
from acloud import ACloudServer
from conf import SERVER, USER, PASSWORD
class TestApiCloud(unittest.TestCase):
def setUp(self):
self.server = ACloudServer(SERVER)
print(f'In method: {self._testMethodName}')
def test_unauthorized_user(self):
expected = False
result = self.server.login('noexiste@email.com', 'p')
self.assertEqual(expected, result)
def test_authorized_user(self):
expected = True
result = self.server.login(USER, PASSWORD)
self.assertEqual(expected, result)
if __name__ == '__main__':
unittest.main()