def test_get_remote_file(self):
        content = str(self.script.read())
        remote_file = {
            'storageAddress': 'localhost',
            'hash': compute_hash(content)
        }

        with mock.patch(
                'substrapp.utils.get_computed_hash') as mget_computed_hash:
            pkhash = compute_hash(content)
            mget_computed_hash.return_value = content, pkhash

            content_remote, pkhash_remote = get_remote_file(remote_file)
            self.assertEqual(pkhash_remote, get_hash(self.script))
            self.assertEqual(content_remote, content)

        with mock.patch(
                'substrapp.utils.get_computed_hash') as mget_computed_hash:
            content = content + ' FAIL'
            pkhash = compute_hash(content)
            mget_computed_hash.return_value = content, pkhash

            with self.assertRaises(Exception):
                get_remote_file(
                    remote_file)  # contents (by pkhash) are different
 def test_get_head_model_ok(self):
     checksum = compute_hash(self.model.read(), key='key_traintuple')
     head_model = Model.objects.create(file=self.model, checksum=checksum, validated=True)
     permissions = {
         "owner": TEST_ORG,
         "permissions": {
             "process": {
                 "public": False,
                 "authorized_ids": ['substra']
             }
         }
     }
     with mock.patch('substrapp.views.utils.get_owner', return_value=TEST_ORG), \
             mock.patch('substrapp.views.utils.get_object_from_ledger') \
             as mget_object_from_ledger, \
             mock.patch('substrapp.views.model.type') as mtype:
         mget_object_from_ledger.return_value = permissions
         mtype.return_value = NodeUser
         extra = {
             'HTTP_SUBSTRA_CHANNEL_NAME': 'mychannel',
             'HTTP_ACCEPT': 'application/json;version=0.0',
         }
         response = self.client.get(f'/model/{head_model.key}/file/', **extra)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
         self.assertEqual(head_model.checksum, compute_hash(response.getvalue(), key='key_traintuple'))
    def test_get_models(self):
        model_content = self.model.read().encode()
        models_content = [model_content, model_content + b', -2.0']

        traintupleKey = compute_hash(models_content[0])
        model_hash = compute_hash(models_content[0], traintupleKey)

        traintupleKey2 = compute_hash(models_content[1])
        model_hash2 = compute_hash(models_content[1], traintupleKey2)

        models_hash = [model_hash, model_hash2]
        model_type = 'inModels'
        subtuple = {
            model_type: [{
                'hash': model_hash,
                'traintupleKey': traintupleKey
            }, {
                'hash': model_hash2,
                'traintupleKey': traintupleKey2
            }]
        }

        with mock.patch('substrapp.tasks.get_remote_file') as mget_remote_file:
            mget_remote_file.side_effect = [[
                models_content[0], models_hash[0]
            ], [models_content[1], models_hash[1]]]
            models_content_res, models_hash_res = get_models(subtuple)

        self.assertEqual(models_content_res, models_content)
        self.assertIsNotNone(models_hash_res, models_hash)
    def test_put_model(self):

        model_content = self.model.read().encode()

        traintupleKey = compute_hash(model_content)
        model_hash = compute_hash(model_content, traintupleKey)
        model_type = 'model'
        subtuple = {
            'key': model_hash,
            model_type: {
                'hash': model_hash,
                'traintupleKey': traintupleKey
            }
        }

        model_directory = os.path.join(self.subtuple_path, 'model')
        create_directory(model_directory)
        put_model(subtuple, self.subtuple_path, model_content)

        model_path = os.path.join(model_directory, traintupleKey)
        self.assertTrue(os.path.exists(model_path))

        os.rename(model_path, model_path + '-local')
        with mock.patch('substrapp.models.Model.objects.get') as mget:
            mget.return_value = FakeModel(model_path + '-local')
            put_model(subtuple, self.subtuple_path, model_content)
            self.assertTrue(os.path.exists(model_path))

        with mock.patch('substrapp.models.Model.objects.get') as mget:
            mget.return_value = FakeModel(model_path)
            with self.assertRaises(Exception):
                put_model({'model': {
                    'hash': 'fail-hash'
                }}, self.subtuple_path, model_content)
    def test_put_models(self):

        model_content = self.model.read().encode()
        models_content = [model_content, model_content + b', -2.0']

        traintupleKey = compute_hash(models_content[0])
        model_hash = compute_hash(models_content[0], traintupleKey)

        traintupleKey2 = compute_hash(models_content[1])
        model_hash2 = compute_hash(models_content[1], traintupleKey2)

        model_path = os.path.join(self.subtuple_path, 'model', traintupleKey)
        model_path2 = os.path.join(self.subtuple_path, 'model', traintupleKey2)

        model_type = 'inModels'
        subtuple = {
            model_type: [{
                'hash': model_hash,
                'traintupleKey': traintupleKey
            }, {
                'hash': model_hash2,
                'traintupleKey': traintupleKey2
            }]
        }

        model_directory = os.path.join(self.subtuple_path, 'model/')

        create_directory(model_directory)
        put_models(subtuple, self.subtuple_path, models_content)

        self.assertTrue(os.path.exists(model_path))
        self.assertTrue(os.path.exists(model_path2))

        os.rename(model_path, model_path + '-local')
        os.rename(model_path2, model_path2 + '-local')

        with mock.patch('substrapp.models.Model.objects.get') as mget:
            mget.side_effect = [
                FakeModel(model_path + '-local'),
                FakeModel(model_path2 + '-local')
            ]
            put_models(subtuple, self.subtuple_path, models_content)

            self.assertTrue(os.path.exists(model_path))
            self.assertTrue(os.path.exists(model_path2))

        with mock.patch('substrapp.models.Model.objects.get') as mget:
            mget.return_value = FakeModel(model_path)
            with self.assertRaises(Exception):
                put_models({'inModels': [{
                    'hash': 'hash'
                }]}, self.subtuple_path, model_content)
    def test_get_remote_file_content(self):
        content = str(self.script.read())
        pkhash = compute_hash(content)
        remote_file = {
            'storageAddress': 'localhost',
            'hash': pkhash,
            'owner': 'external_node_id',
        }

        with mock.patch('substrapp.utils.get_owner') as get_owner,\
                mock.patch('substrapp.utils.requests.get') as request_get:
            get_owner.return_value = 'external_node_id'
            request_get.return_value = FakeRequest(content=content,
                                                   status=status.HTTP_200_OK)

            content_remote = get_remote_file_content(remote_file,
                                                     'external_node_id',
                                                     pkhash)
            self.assertEqual(content_remote, content)

        with mock.patch('substrapp.utils.get_owner') as get_owner,\
                mock.patch('substrapp.utils.requests.get') as request_get:
            get_owner.return_value = 'external_node_id'
            request_get.return_value = FakeRequest(content=content,
                                                   status=status.HTTP_200_OK)

            with self.assertRaises(Exception):
                # contents (by pkhash) are different
                get_remote_file_content(remote_file, 'external_node_id',
                                        'fake_pkhash')
    def test_get_model(self):
        model_content = self.model.read().encode()
        traintupleKey = compute_hash(model_content)
        model_hash = compute_hash(model_content, traintupleKey)
        model_type = 'model'
        subtuple = {
            model_type: {
                'hash': model_hash,
                'traintupleKey': traintupleKey
            }
        }

        with mock.patch('substrapp.tasks.get_remote_file') as mget_remote_file:
            mget_remote_file.return_value = model_content, model_hash
            model_content, model_hash = get_model(subtuple)

        self.assertIsNotNone(model_content)
        self.assertIsNotNone(model_hash)
    def test_get_algo_files(self):
        algo = Algo.objects.create(file=self.algo)
        with mock.patch('substrapp.views.utils.get_owner', return_value='foo'), \
                mock.patch('substrapp.views.utils.get_object_from_ledger') \
                as mget_object_from_ledger:
            mget_object_from_ledger.return_value = get_sample_algo_metadata()

            extra = {
                'HTTP_ACCEPT': 'application/json;version=0.0',
            }
            response = self.client.get(f'/algo/{algo.pkhash}/file/', **extra)
            self.assertEqual(response.status_code, status.HTTP_200_OK)
            self.assertEqual(algo.pkhash, compute_hash(response.getvalue()))
    def test_objective_retrieve(self):
        url = reverse('substrapp:objective-list')

        with mock.patch('substrapp.views.objective.get_object_from_ledger') as mget_object_from_ledger, \
                mock.patch('substrapp.views.objective.get_remote_asset') as get_remote_asset:
            mget_object_from_ledger.return_value = objective[0]

            with open(
                    os.path.join(
                        os.path.dirname(os.path.realpath(__file__)),
                        '../../../../fixtures/owkin/objectives/objective0/description.md'
                    ), 'rb') as f:
                content = f.read()

            get_remote_asset.return_value = content

            pkhash = compute_hash(content)
            search_params = f'{pkhash}/'

            response = self.client.get(url + search_params, **self.extra)
            r = response.json()

            self.assertEqual(r, objective[0])