'''
    Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    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 unittest
import json

from . import BaseTestCase


class TestUsers(BaseTestCase):
    @classmethod
    def setUpClass(self):
        self.base_url = '/mapservice/v1/user'

    def test_users_get(self):
        """Test case for getting users (src)
		"""
        headers = {}
        response = self.client.open(
            self.base_url, method='GET', headers=headers
        )
        assert response.status_code == 401

        response = self.client.open(
            self.base_url, method='GET', headers={'X-API-KEY': 'test_key'}
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        self.check_key("username", "autotests", data)
        self.check_key("firstName", "Autotests", data)
        self.check_key("lastName", "", data)
        self.check_key("email", "auto@example.org", data)
        self.check_key("activated", True, data)
        self.check_key("licenseAccepted", True, data)

        assert "apiKeys" in data
        assert data["apiKeys"][0] == "test_key"
        assert data["apiKeys"][1] == "test_key_delete"

        assert "password" not in data
        assert "salt" not in data
        assert "activationCode" not in data

    def test_users_register(self):
        """Test case for users_register (Src)
			Register a new user
		"""
        user_creation = {
            "username": "jane_doe",
            "firstName": "Jane",
            "lastName": "Doe",
            "password": "1234",
            "licenseAccepted": True,
            "email": "jane.doe@example.org"
        }
        headers = {
            'Accept': 'application/json',
        }
        response = self.client.open(
            self.base_url,
            method='POST',
            headers=headers,
            data=json.dumps(user_creation),
            content_type='application/json'
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        self.check_key("username", user_creation['username'], data)
        self.check_key("firstName", user_creation['firstName'], data)
        self.check_key("lastName", user_creation['lastName'], data)
        self.check_key("email", user_creation['email'], data)
        self.check_key("activated", False, data)
        self.check_key("licenseAccepted", True, data)

        assert 'apiKeys' in data
        assert len(data['apiKeys']) == 0


if __name__ == '__main__':
    unittest.main()
