class APIDownloadTest(TestCase):
    def setUp(self):
        search = settings.SEARCH
        path_to_save = './tmp-tests/'
        login = {"username": settings.USERNAME, "password": settings.PASSWORD}
        host = "http://www2.inpe.br/climaespacial/SpaceWeatherDataShare"
        self.download = APIDownload(credentials=login,
                                    host=host,
                                    path=path_to_save,
                                    **search)

    def test_login(self):
        """The login must return a token"""
        token = self.download.get_token()
        self.assertIsInstance(token, str)

    def test_token_format(self):
        """The token needs be with a Bearer prefix"""
        bearer_token = self.download.get_authorization()
        bearer = bearer_token.split(' ')[0]
        self.assertEqual('Bearer', bearer)

    def test_get_files_list(self):
        """Must return a files urls list"""
        files = self.download.get_files_list()
        self.assertTrue(len(files) > 0)
 def setUp(self):
     search = settings.SEARCH
     path_to_save = './tmp-tests/'
     login = {"username": settings.USERNAME, "password": settings.PASSWORD}
     host = "http://www2.inpe.br/climaespacial/SpaceWeatherDataShare"
     self.download = APIDownload(credentials=login,
                                 host=host,
                                 path=path_to_save,
                                 **search)
 def test_credentials_has_password_key(self):
     """The credentials must has the password key"""
     with self.assertRaises(SystemExit) as cm:
         APIDownload(credentials={'username': '******'},
                     host='http://google.com')
     error_msg = 'The login dictonary must has a key called password'
     self.assertEqual(cm.exception.args[0].msg, error_msg)
 def test_host_is_valid(self):
     """The host must be a valid host"""
     with self.assertRaises(SystemExit) as cm:
         APIDownload(credentials={},
                     host='http://www.donotexistdomainbyswds.com')
     error_msg = 'The variable host is not a valid URL e.g: "http://apidomain.com"'
     self.assertEqual(cm.exception.args[0].msg, error_msg)
 def test_credentials_password_key_not_empty(self):
     """The credentials password key must be not empty"""
     with self.assertRaises(SystemExit) as cm:
         APIDownload(credentials={
             'password': '',
             'username': '******'
         },
                     host='http://google.com')
     error_msg = 'The password key on login dictonary cannot be empty'
     self.assertEqual(cm.exception.args[0].msg, error_msg)
def main(argv):
    h.log_config()

    if len(argv) == 1:
        search = settings.SEARCH
        path_to_save = settings.PATH_TO_SAVE

    else:
        search, path_to_save = h.get_sys_args(argv[1:])

    host = 'http://www2.inpe.br/climaespacial/SpaceWeatherDataShare'

    login = {
        "username": settings.USERNAME,
        "password": settings.PASSWORD
    }

    download = APIDownload(credentials=login, host=host, path=path_to_save, **search)
    files = download.get_files_list()
    download.download_files(files)
 def test_host_not_empty(self):
     """The host cannot be empty"""
     with self.assertRaises(SystemExit) as cm:
         APIDownload(credentials={}, host='')
     error_msg = 'The variable host needs an URL value'
     self.assertEqual(cm.exception.args[0].msg, error_msg)