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

###############################################################################
#  Copyright Kitware Inc.
#
#  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.
###############################################################################

import os

# Need to set the environment variable before importing girder
os.environ['GIRDER_PORT'] = os.environ.get('GIRDER_TEST_PORT', '20200')  # noqa

from tests import base


def setUpModule():
    """
    Enable the minerva plugin and start the server.
    """
    base.enabledPlugins.append('jobs')
    base.enabledPlugins.append('romanesco')
    base.enabledPlugins.append('gravatar')
    base.enabledPlugins.append('minerva')
    base.startServer(False)


def tearDownModule():
    """
    Stop the server.
    """
    base.stopServer()


class WmsTestCase(base.TestCase):

    """
    Tests of the minerva source API endpoints.
    """

    def setUp(self):
        """
        Set up the test case with  a user
        """
        super(WmsTestCase, self).setUp()

        self._user = self.model('user').createUser(
            'minervauser', 'password', '<PASSWORD>', 'user',
            '<EMAIL>')

    def testCreateWmsSource(self):
        """
        Test the minerva WMS source API endpoints.
        """

        path = '/minerva_source_wms'
        name = 'testWMS'
        username = ''
        password = ''
        baseURL = 'http://demo.boundlessgeo.com/geoserver/ows'
        params = {
            'name': name,
            'username': username,
            'password': password,
            'baseURL': baseURL
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsSource = response.json
        minerva_metadata = wmsSource['meta']['minerva']
        self.assertEquals(wmsSource['name'], name, 'incorrect wms source name')
        self.assertEquals(minerva_metadata['source_type'], 'wms', 'incorrect wms source type')
        self.assertEquals(minerva_metadata['wms_params']['base_url'], baseURL, 'incorrect wms source baseURL')

    def testCreateWmsDataset(self):
        """
        Test the minerva WMS dataset API endpoints.
        """

        # create the source

        path = '/minerva_source_wms'
        name = 'testWMS'
        typeName = 'geonode:global_temp'
        username = ''
        password = ''
        baseURL = 'http://demo.boundlessgeo.com/geoserver/ows'
        params = {
            'name': name,
            'username': username,
            'password': password,
            'baseURL': baseURL
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsSource = response.json

        # create the dataset

        path = '/minerva_dataset_wms'
        name = 'testWMSdataset'
        wmsParams = {}
        params = {
            'name': name,
            'wmsSourceId': wmsSource['_id'],
            'typeName': typeName,
            'wmsParams': wmsParams
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsDataset = response.json
        minerva_metadata = wmsDataset['meta']['minerva']
        self.assertEquals(wmsDataset['name'], name, 'incorrect wms dataset name')
        self.assertEquals(minerva_metadata['source_id'], wmsSource['_id'], 'incorrect wms source_id')
        self.assertEquals(minerva_metadata['dataset_type'], 'wms', 'incorrect wms dataset type')
        self.assertEquals(minerva_metadata['base_url'], wmsSource['meta']['minerva']['wms_params']['base_url'],'incorrect wms dataset baseURL')
        self.assertEquals(minerva_metadata['type_name'], typeName, 'incorrect wms dataset typeName')
        legend = '<KEY>'
        # TODO determine if legend is stable, if not, needs to be mocked
        self.assertEquals(minerva_metadata['legend'], legend, 'incorrect wms dataset legend')<commit_msg>Add a test for WMS source wqauthentication encryption/decryption<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  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.
###############################################################################

import os

# Need to set the environment variable before importing girder
os.environ['GIRDER_PORT'] = os.environ.get('GIRDER_TEST_PORT', '20200')  # noqa

from tests import base
from cryptography.fernet import Fernet
from girder.utility import config


def setUpModule():
    """
    Enable the minerva plugin and start the server.
    """
    base.enabledPlugins.append('jobs')
    base.enabledPlugins.append('romanesco')
    base.enabledPlugins.append('gravatar')
    base.enabledPlugins.append('minerva')
    base.startServer(False)


def tearDownModule():
    """
    Stop the server.
    """
    base.stopServer()


class WmsTestCase(base.TestCase):

    """
    Tests of the minerva source API endpoints.
    """

    def setUp(self):
        """
        Set up the test case with  a user
        """
        super(WmsTestCase, self).setUp()

        self._user = self.model('user').createUser(
            'minervauser', 'password', '<PASSWORD>', 'user',
            '<EMAIL>')

    def testCreateWmsSource(self):
        """
        Test the minerva WMS source API endpoints.
        """

        path = '/minerva_source_wms'
        name = 'testWMS'
        username = ''
        password = ''
        baseURL = 'http://demo.boundlessgeo.com/geoserver/ows'
        params = {
            'name': name,
            'username': username,
            'password': password,
            'baseURL': baseURL
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsSource = response.json
        minerva_metadata = wmsSource['meta']['minerva']
        self.assertEquals(wmsSource['name'], name, 'incorrect wms source name')
        self.assertEquals(minerva_metadata['source_type'], 'wms', 'incorrect wms source type')
        self.assertEquals(minerva_metadata['wms_params']['base_url'], baseURL, 'incorrect wms source baseURL')

    def testCreateWmsDataset(self):
        """
        Test the minerva WMS dataset API endpoints.
        """

        # create the source

        path = '/minerva_source_wms'
        name = 'testWMS'
        typeName = 'geonode:global_temp'
        username = ''
        password = ''
        baseURL = 'http://demo.boundlessgeo.com/geoserver/ows'
        params = {
            'name': name,
            'username': username,
            'password': password,
            'baseURL': baseURL
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsSource = response.json

        # create the dataset

        path = '/minerva_dataset_wms'
        name = 'testWMSdataset'
        wmsParams = {}
        params = {
            'name': name,
            'wmsSourceId': wmsSource['_id'],
            'typeName': typeName,
            'wmsParams': wmsParams
        }
        response = self.request(path=path, method='POST', params=params, user=self._user)
        self.assertStatusOk(response)
        wmsDataset = response.json
        minerva_metadata = wmsDataset['meta']['minerva']
        self.assertEquals(wmsDataset['name'], name, 'incorrect wms dataset name')
        self.assertEquals(minerva_metadata['source_id'], wmsSource['_id'], 'incorrect wms source_id')
        self.assertEquals(minerva_metadata['dataset_type'], 'wms', 'incorrect wms dataset type')
        self.assertEquals(minerva_metadata['base_url'], wmsSource['meta']['minerva']['wms_params']['base_url'],'incorrect wms dataset baseURL')
        self.assertEquals(minerva_metadata['type_name'], typeName, 'incorrect wms dataset typeName')
        legend = '<KEY>RXhjZXB0aW9uUmVwb3J0Pg==\n'
        # TODO determine if legend is stable, if not, needs to be mocked
        self.assertEquals(minerva_metadata['legend'], legend, 'incorrect wms dataset legend')

    def testCreateWmsSourceWithAuthentication(self):
        """
        Enter a username and password for a WMS source and ensure that they
        are correctly encrypted/decrypted.
        """

        # create the source
        path = '/minerva_source_wms'
        name = 'testWMSAuth'
        username = 'admin'
        password = '<PASSWORD>'
        baseURL = 'http://demo.geonode.org/geoserver/wms'
        params = {
            'name': name,
            'username': username,
            'password': password,
            'baseURL': baseURL
        }
        response = self.request(path=path, method='POST',
                                params=params, user=self._user)
        self.assertStatusOk(response)
        wmsSource = response.json
        credentials = wmsSource['meta']['minerva']['wms_params']['credentials']
        #Make sure credentials were encrypted
        self.assertNotEqual(credentials, '{}:{}'.format(username, password),
                            "Credentials were not encrypted!")
        #Redo the decryptCredentials function here because importing from
        #minerva_utility breaks on 'from girder.plugins.minerva.constants'
        cur_config = config.getConfig()
        key = cur_config['minerva']['crypto_key']
        f = Fernet(key)
        decrypted = f.decrypt(bytes(credentials))
        #Make sure credentials were correctly decrypted
        self.assertEquals(decrypted, '{}:{}'.format(username,password),
                          'Credentials could not be correctly decrypted')

{'fraction_non_alphanumeric': 0.14166897698918768, 'fraction_numerical': 0.0022179096201829776, 'mean_word_length': 2.7498267498267497, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2193513', 'n_tokens_mistral': 3069, 'n_tokens_neox': 3030, 'n_words': 753}