# Testing SharePoint

SharePoint mocks - sites, lists, items, columns and subsites.

Mock SharePoint connections to test services that work with SharePoint sites, lists, and items.

**Note:** If you're new to unit testing with Zato, check the [tutorial](https://zato.io/docs/api-testing/tutorial.html) first.

## Getting list items {#getting-list-items}

**The service**:

```python
from zato.server.service import Service

class GetSuppliers(Service):
    name = 'sharepoint.suppliers.get'

    def handle(self):
        conn = self.microsoft.cloud['ms365.Sharepoint']

        site = conn.sharepoint().get_site('sites/procurement')
        sp_list = site.get_list_by_name('Suppliers')
        items = sp_list.get_items()

        self.response.payload = {
            'suppliers': [{'name': item['Title'], 'status': item['Status']} for item in items]
        }
```

**The test**:

```python
from zato_testing import ServiceTestCase
from myapp.services import GetSuppliers

class TestGetSuppliers(ServiceTestCase):

    def test_returns_suppliers(self):

        # Configure responses for the method chain
        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name', {'id': 'list-456'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_items', [
            {'Title': 'Acme Corp', 'Status': 'Active'},
            {'Title': 'Global Inc', 'Status': 'Active'},
            {'Title': 'Local Ltd', 'Status': 'Inactive'}
        ])

        service = self.invoke(GetSuppliers)

        suppliers = service.response.payload['suppliers']
        self.assertEqual(len(suppliers), 3)
        self.assertEqual(suppliers[0]['name'], 'Acme Corp')
```

## Creating list items {#creating-list-items}

**The service**:

```python
class CreateSupplier(Service):
    name = 'sharepoint.supplier.create'
    input = 'name', 'status'

    def handle(self):
        conn = self.microsoft.cloud['ms365.Sharepoint']

        site = conn.sharepoint().get_site('sites/procurement')
        sp_list = site.get_list_by_name('Suppliers')
        result = sp_list.create_list_item({
            'Title': self.request.input.name,
            'Status': self.request.input.status
        })

        self.response.payload = {'id': result['id'], 'created': True}
```

**The test**:

```python
class TestCreateSupplier(ServiceTestCase):

    def test_creates_supplier(self):

        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name', {'id': 'list-456'})
        self.set_response(
            'ms365.Sharepoint.sharepoint.get_site.get_list_by_name.create_list_item',
            response={'id': 'item-789'},
            request={'Title': 'New Supplier', 'Status': 'Active'}
        )

        service = self.invoke(CreateSupplier, name='New Supplier', status='Active')

        self.assertEqual(service.response.payload['id'], 'item-789')
        self.assertEqual(service.response.payload['created'], True)
```

## Updating list items {#updating-list-items}

**The service**:

```python
class UpdateSupplier(Service):
    name = 'sharepoint.supplier.update'
    input = 'item_id', 'status'

    def handle(self):
        conn = self.microsoft.cloud['ms365.Sharepoint']

        site = conn.sharepoint().get_site('sites/procurement')
        sp_list = site.get_list_by_name('Suppliers')
        item = sp_list.get_item_by_id(self.request.input.item_id)
        item.update_fields({'Status': self.request.input.status})
        result = item.save_updates()

        self.response.payload = {'updated': result}
```

**The test**:

```python
class TestUpdateSupplier(ServiceTestCase):

    def test_updates_supplier(self):

        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name', {'id': 'list-456'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_item_by_id', {
            'id': 'item-789',
            'Title': 'Acme Corp'
        })
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_item_by_id.update_fields', None)
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_item_by_id.save_updates', True)

        service = self.invoke(UpdateSupplier, item_id='item-789', status='Inactive')

        self.assertEqual(service.response.payload['updated'], True)
```

## Getting list columns {#getting-list-columns}

```python
class TestGetColumns(ServiceTestCase):

    def test_returns_columns(self):

        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name', {'id': 'list-456'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_list_by_name.get_list_columns', [
            {'name': 'Title', 'type': 'text'},
            {'name': 'Status', 'type': 'choice'},
            {'name': 'Amount', 'type': 'number'}
        ])

        service = self.invoke(GetListColumns)

        self.assertEqual(len(service.response.payload['columns']), 3)
```

## Getting subsites {#getting-subsites}

```python
class TestGetSubsites(ServiceTestCase):

    def test_returns_subsites(self):

        self.set_response('ms365.Sharepoint.sharepoint.get_site', {'id': 'site-123'})
        self.set_response('ms365.Sharepoint.sharepoint.get_site.get_subsites', [
            {'name': 'Subsite A', 'url': 'sites/main/subsite-a'},
            {'name': 'Subsite B', 'url': 'sites/main/subsite-b'}
        ])

        service = self.invoke(GetSubsites)

        self.assertEqual(len(service.response.payload['subsites']), 2)
```

## Learn more {#learn-more}

- [API testing](https://zato.io/docs/api-testing/) - Tests in plain English, run against real or mocked connections
- [API testing tutorial](https://zato.io/docs/api-testing/tutorial.html) - A complete test suite, from the first scenario to the report
- [Testing REST connections](https://zato.io/docs/api-testing/rest.html) - Mock outgoing REST calls and assert on what your service sent
- [Testing SQL connections](https://zato.io/docs/api-testing/sql.html) - Mock database queries and the rows they return
- [Testing LDAP connections](https://zato.io/docs/api-testing/ldap.html) - Mock directory lookups and the entries they find
- [Testing Microsoft 365](https://zato.io/docs/api-testing/ms365/) - Mock mailboxes, calendars, SharePoint, Teams and the rest
- [Testing service invocations](https://zato.io/docs/api-testing/services.html) - Services that call other services, with the callees mocked
- [Testing Jira connections](https://zato.io/docs/api-testing/jira.html) - Mock Jira API calls and the issues they return
- [Tests in CI/CD](https://zato.io/docs/api-testing/ci-cd.html) - Running the suite in a pipeline and reading its output
