import pytest
from ckan.tests.factories import User, Dataset, Organization
from ckan.plugins.toolkit import get_action


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_allowed_organization_user_should_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    organization2 = Organization(users=org2_users)

    dataset = Dataset(private=True, owner_org=organization1['id'], allowed_organizations=organization2['name'])

    results = get_action(u"package_search")(
            {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == [dataset['name']]

import pytest
from ckan.tests.factories import User, Dataset, Organization
from ckan.plugins.toolkit import get_action


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_allowed_organization_user_should_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    organization2 = Organization(users=org2_users)

    dataset = Dataset(private=True, owner_org=organization1['id'], allowed_organizations=organization2['name'])

    results = get_action(u"package_search")(
            {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == [dataset['name']]


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_not_allowed_organization_user_should_not_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    Organization(users=org2_users)

    Dataset(private=True, owner_org=organization1['id'], allowed_organizations="")

    results = get_action(u"package_search")(
        {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == []

Add not allowed organization test
LIKA-410: Add not allowed organization test

  import pytest
  from ckan.tests.factories import User, Dataset, Organization
  from ckan.plugins.toolkit import get_action
  
  
  @pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
  def test_allowed_organization_user_should_see_subsystem():
      organization1 = Organization()
  
      user2 = User()
      org2_users = [{"name": user2["name"], "capacity": "admin"}]
  
      organization2 = Organization(users=org2_users)
  
      dataset = Dataset(private=True, owner_org=organization1['id'], allowed_organizations=organization2['name'])
  
      results = get_action(u"package_search")(
              {u"user": user2["name"]}, {u"include_private": True}
      )["results"]
  
      names = [r["name"] for r in results]
      assert names == [dataset['name']]
  
+ 
+ @pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
+ def test_not_allowed_organization_user_should_not_see_subsystem():
+     organization1 = Organization()
+ 
+     user2 = User()
+     org2_users = [{"name": user2["name"], "capacity": "admin"}]
+ 
+     Organization(users=org2_users)
+ 
+     Dataset(private=True, owner_org=organization1['id'], allowed_organizations="")
+ 
+     results = get_action(u"package_search")(
+         {u"user": user2["name"]}, {u"include_private": True}
+     )["results"]
+ 
+     names = [r["name"] for r in results]
+     assert names == []
+ 
Add not allowed organization test
## Code Before:
import pytest
from ckan.tests.factories import User, Dataset, Organization
from ckan.plugins.toolkit import get_action


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_allowed_organization_user_should_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    organization2 = Organization(users=org2_users)

    dataset = Dataset(private=True, owner_org=organization1['id'], allowed_organizations=organization2['name'])

    results = get_action(u"package_search")(
            {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == [dataset['name']]

## Instruction:
Add not allowed organization test
## Code After:
import pytest
from ckan.tests.factories import User, Dataset, Organization
from ckan.plugins.toolkit import get_action


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_allowed_organization_user_should_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    organization2 = Organization(users=org2_users)

    dataset = Dataset(private=True, owner_org=organization1['id'], allowed_organizations=organization2['name'])

    results = get_action(u"package_search")(
            {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == [dataset['name']]


@pytest.mark.usefixtures('with_plugins', 'clean_db', 'clean_index')
def test_not_allowed_organization_user_should_not_see_subsystem():
    organization1 = Organization()

    user2 = User()
    org2_users = [{"name": user2["name"], "capacity": "admin"}]

    Organization(users=org2_users)

    Dataset(private=True, owner_org=organization1['id'], allowed_organizations="")

    results = get_action(u"package_search")(
        {u"user": user2["name"]}, {u"include_private": True}
    )["results"]

    names = [r["name"] for r in results]
    assert names == []
