def test_update_organization_config_remove_all(self): self.assert_installation() model = Integration.objects.get(provider='vsts') integration = VstsIntegration(model, self.organization.id) org_integration = OrganizationIntegration.objects.get( organization_id=self.organization.id, ) data = { 'sync_status_forward': {}, 'other_option': 'hello', } IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=1, resolved_status='ResolvedStatus1', unresolved_status='UnresolvedStatus1', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=2, resolved_status='ResolvedStatus2', unresolved_status='UnresolvedStatus2', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=3, resolved_status='ResolvedStatus3', unresolved_status='UnresolvedStatus3', ) integration.update_organization_config(data) external_projects = IntegrationExternalProject.objects \ .all() \ .values_list('external_id', flat=True) assert list(external_projects) == [] config = OrganizationIntegration.objects.get( organization_id=org_integration.organization_id, integration_id=org_integration.integration_id ).config assert config == { 'sync_status_forward': False, 'other_option': 'hello', }
def test_update_domain_name(self): account_name = 'MyVSTSAccount.visualstudio.com' account_uri = 'https://MyVSTSAccount.visualstudio.com/' self.assert_installation() model = Integration.objects.get(provider='vsts') model.metadata['domain_name'] = account_name model.save() integration = VstsIntegration(model, self.organization.id) integration.get_client() domain_name = integration.model.metadata['domain_name'] assert domain_name == account_uri assert Integration.objects.get(provider='vsts').metadata['domain_name'] == account_uri
def setUp(self): organization = self.create_organization() project = self.create_project(organization=organization) self.access_token = '1234567890' self.instance = 'instance.visualstudio.com' self.model = Integration.objects.create( provider='integrations:vsts', external_id='vsts_external_id', name='vsts_name', metadata={ 'domain_name': 'instance.visualstudio.com' } ) self.identity_provider = IdentityProvider.objects.create(type='vsts') self.identity = Identity.objects.create( idp=self.identity_provider, user=self.user, external_id='vsts_id', data={ 'access_token': self.access_token, 'refresh_token': 'qwertyuiop', 'expires': int(time()) - int(1234567890), } ) self.org_integration = self.model.add_organization(organization.id, self.identity.id) self.project_integration = self.model.add_project(project.id) self.integration = VstsIntegration(self.model, organization.id, project.id) self.projects = [ ('eb6e4656-77fc-42a1-9181-4c6d8e9da5d1', 'ProjectB'), ('6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c', 'ProjectA') ] self.project_result = { 'value': [ { 'id': self.projects[0][0], 'name': self.projects[0][1], }, { 'id': self.projects[1][0], 'name': self.projects[1][1], } ], 'count': 2 } responses.add( responses.GET, 'https://instance.visualstudio.com/DefaultCollection/_apis/projects', json=self.project_result, ) self.refresh_data = { 'access_token': 'access token for this user', 'token_type': 'type of token', 'expires_in': 123456789, 'refresh_token': 'new refresh token to use when the token has timed out', } responses.add( responses.POST, 'https://app.vssps.visualstudio.com/oauth2/token', json=self.refresh_data, )
class VstsIntegrationTest(APITestCase): def setUp(self): organization = self.create_organization() project = self.create_project(organization=organization) self.access_token = '1234567890' self.instance = 'instance.visualstudio.com' self.model = Integration.objects.create( provider='integrations:vsts', external_id='vsts_external_id', name='vsts_name', metadata={ 'domain_name': 'instance.visualstudio.com' } ) self.identity_provider = IdentityProvider.objects.create(type='vsts') self.identity = Identity.objects.create( idp=self.identity_provider, user=self.user, external_id='vsts_id', data={ 'access_token': self.access_token, 'refresh_token': 'qwertyuiop', 'expires': int(time()) - int(1234567890), } ) self.org_integration = self.model.add_organization(organization.id, self.identity.id) self.project_integration = self.model.add_project(project.id) self.integration = VstsIntegration(self.model, organization.id, project.id) self.projects = [ ('eb6e4656-77fc-42a1-9181-4c6d8e9da5d1', 'ProjectB'), ('6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c', 'ProjectA') ] self.project_result = { 'value': [ { 'id': self.projects[0][0], 'name': self.projects[0][1], }, { 'id': self.projects[1][0], 'name': self.projects[1][1], } ], 'count': 2 } responses.add( responses.GET, 'https://instance.visualstudio.com/DefaultCollection/_apis/projects', json=self.project_result, ) self.refresh_data = { 'access_token': 'access token for this user', 'token_type': 'type of token', 'expires_in': 123456789, 'refresh_token': 'new refresh token to use when the token has timed out', } responses.add( responses.POST, 'https://app.vssps.visualstudio.com/oauth2/token', json=self.refresh_data, ) def assert_identity_updated(self, new_identity, expected_data): assert new_identity.data['access_token'] == expected_data['access_token'] assert new_identity.data['token_type'] == expected_data['token_type'] assert new_identity.data['refresh_token'] == expected_data['refresh_token'] assert new_identity.data['expires'] >= time() def test_get_client(self): client = self.integration.get_client() assert client.identity.data['access_token'] == self.access_token @responses.activate def test_refreshes_expired_token(self): result = self.integration.get_client().get_projects(self.instance) assert len(responses.calls) == 2 default_identity = self.integration.default_identity self.assert_identity_updated(default_identity, self.refresh_data) identity = Identity.objects.get(id=self.identity.id) self.assert_identity_updated(identity, self.refresh_data) projects = result['value'] assert projects[0]['id'] == self.projects[0][0] and projects[0]['name'] == self.projects[0][1] assert projects[1]['id'] == self.projects[1][0] and projects[1]['name'] == self.projects[1][1] @responses.activate def test_get_organization_config(self): fields = self.integration.get_organization_config() assert len(fields) == 6 names = [ 'resolve_status', 'resolve_when', 'regression_status', 'sync_comments', 'sync_forward_assignment', 'sync_reverse_assignment'] assert [field['name'] for field in fields] == names
def test_update_organization_config(self): self.assert_installation() org_integration = OrganizationIntegration.objects.get( organization_id=self.organization.id, ) model = Integration.objects.get(provider='vsts') integration = VstsIntegration(model, self.organization.id) # test validation data = { 'sync_status_forward': { 1: { 'on_resolve': '', 'on_unresolve': 'UnresolvedStatus1', }, }, } with self.assertRaises(IntegrationError): integration.update_organization_config(data) data = { 'sync_status_forward': { 1: { 'on_resolve': 'ResolvedStatus1', 'on_unresolve': 'UnresolvedStatus1', }, 2: { 'on_resolve': 'ResolvedStatus2', 'on_unresolve': 'UnresolvedStatus2', }, 4: { 'on_resolve': 'ResolvedStatus4', 'on_unresolve': 'UnresolvedStatus4', }, }, 'other_option': 'hello', } IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=1, resolved_status='UpdateMe', unresolved_status='UpdateMe', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=2, resolved_status='ResolvedStatus2', unresolved_status='UnresolvedStatus2', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=3, resolved_status='ResolvedStatus3', unresolved_status='UnresolvedStatus3', ) integration.update_organization_config(data) external_projects = IntegrationExternalProject.objects \ .all() \ .order_by('external_id') assert external_projects[0].external_id == '1' assert external_projects[0].resolved_status == 'ResolvedStatus1' assert external_projects[0].unresolved_status == 'UnresolvedStatus1' assert external_projects[1].external_id == '2' assert external_projects[1].resolved_status == 'ResolvedStatus2' assert external_projects[1].unresolved_status == 'UnresolvedStatus2' assert external_projects[2].external_id == '4' assert external_projects[2].resolved_status == 'ResolvedStatus4' assert external_projects[2].unresolved_status == 'UnresolvedStatus4' config = OrganizationIntegration.objects.get( organization_id=org_integration.organization_id, integration_id=org_integration.integration_id).config assert config == { 'sync_status_forward': True, 'other_option': 'hello', }
def setUp(self): organization = self.create_organization() project = self.create_project(organization=organization) self.access_token = '1234567890' self.instance = 'instance.visualstudio.com' self.model = Integration.objects.create( provider='integrations:vsts', external_id='vsts_external_id', name='vsts_name', metadata={'domain_name': 'instance.visualstudio.com'}) self.identity_provider = IdentityProvider.objects.create(type='vsts') self.identity = Identity.objects.create(idp=self.identity_provider, user=self.user, external_id='vsts_id', data={ 'access_token': self.access_token, 'refresh_token': 'qwertyuiop', 'expires': int(time()) - int(1234567890), }) self.org_integration = self.model.add_organization( organization.id, self.identity.id) self.project_integration = self.model.add_project(project.id) self.integration = VstsIntegration(self.model, organization.id, project.id) self.projects = [('eb6e4656-77fc-42a1-9181-4c6d8e9da5d1', 'ProjectB'), ('6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c', 'ProjectA')] self.project_result = { 'value': [{ 'id': self.projects[0][0], 'name': self.projects[0][1], }, { 'id': self.projects[1][0], 'name': self.projects[1][1], }], 'count': 2 } responses.add( responses.GET, 'https://instance.visualstudio.com/DefaultCollection/_apis/projects', json=self.project_result, ) self.refresh_data = { 'access_token': 'access token for this user', 'token_type': 'type of token', 'expires_in': 123456789, 'refresh_token': 'new refresh token to use when the token has timed out', } responses.add( responses.POST, 'https://app.vssps.visualstudio.com/oauth2/token', json=self.refresh_data, )
class VstsIntegrationTest(APITestCase): def setUp(self): organization = self.create_organization() project = self.create_project(organization=organization) self.access_token = '1234567890' self.instance = 'instance.visualstudio.com' self.model = Integration.objects.create( provider='integrations:vsts', external_id='vsts_external_id', name='vsts_name', metadata={'domain_name': 'instance.visualstudio.com'}) self.identity_provider = IdentityProvider.objects.create(type='vsts') self.identity = Identity.objects.create(idp=self.identity_provider, user=self.user, external_id='vsts_id', data={ 'access_token': self.access_token, 'refresh_token': 'qwertyuiop', 'expires': int(time()) - int(1234567890), }) self.org_integration = self.model.add_organization( organization.id, self.identity.id) self.project_integration = self.model.add_project(project.id) self.integration = VstsIntegration(self.model, organization.id, project.id) self.projects = [('eb6e4656-77fc-42a1-9181-4c6d8e9da5d1', 'ProjectB'), ('6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c', 'ProjectA')] self.project_result = { 'value': [{ 'id': self.projects[0][0], 'name': self.projects[0][1], }, { 'id': self.projects[1][0], 'name': self.projects[1][1], }], 'count': 2 } responses.add( responses.GET, 'https://instance.visualstudio.com/DefaultCollection/_apis/projects', json=self.project_result, ) self.refresh_data = { 'access_token': 'access token for this user', 'token_type': 'type of token', 'expires_in': 123456789, 'refresh_token': 'new refresh token to use when the token has timed out', } responses.add( responses.POST, 'https://app.vssps.visualstudio.com/oauth2/token', json=self.refresh_data, ) def assert_identity_updated(self, new_identity, expected_data): assert new_identity.data['access_token'] == expected_data[ 'access_token'] assert new_identity.data['token_type'] == expected_data['token_type'] assert new_identity.data['refresh_token'] == expected_data[ 'refresh_token'] assert new_identity.data['expires'] >= time() def test_get_client(self): client = self.integration.get_client() assert client.identity.data['access_token'] == self.access_token @responses.activate def test_refreshes_expired_token(self): result = self.integration.get_client().get_projects(self.instance) assert len(responses.calls) == 2 default_identity = self.integration.default_identity self.assert_identity_updated(default_identity, self.refresh_data) identity = Identity.objects.get(id=self.identity.id) self.assert_identity_updated(identity, self.refresh_data) projects = result['value'] assert projects[0]['id'] == self.projects[0][0] and projects[0][ 'name'] == self.projects[0][1] assert projects[1]['id'] == self.projects[1][0] and projects[1][ 'name'] == self.projects[1][1] @responses.activate def test_get_organization_config(self): fields = self.integration.get_organization_config() assert len(fields) == 6 names = [ 'resolve_status', 'resolve_when', 'regression_status', 'sync_comments', 'sync_forward_assignment', 'sync_reverse_assignment' ] assert [field['name'] for field in fields] == names
def test_update_organization_config(self): self.assert_installation() org_integration = OrganizationIntegration.objects.get(organization_id=self.organization.id) model = Integration.objects.get(provider="vsts") integration = VstsIntegration(model, self.organization.id) # test validation data = {"sync_status_forward": {1: {"on_resolve": "", "on_unresolve": "UnresolvedStatus1"}}} with self.assertRaises(IntegrationError): integration.update_organization_config(data) data = { "sync_status_forward": { 1: {"on_resolve": "ResolvedStatus1", "on_unresolve": "UnresolvedStatus1"}, 2: {"on_resolve": "ResolvedStatus2", "on_unresolve": "UnresolvedStatus2"}, 4: {"on_resolve": "ResolvedStatus4", "on_unresolve": "UnresolvedStatus4"}, }, "other_option": "hello", } IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=1, resolved_status="UpdateMe", unresolved_status="UpdateMe", ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=2, resolved_status="ResolvedStatus2", unresolved_status="UnresolvedStatus2", ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=3, resolved_status="ResolvedStatus3", unresolved_status="UnresolvedStatus3", ) integration.update_organization_config(data) external_projects = IntegrationExternalProject.objects.all().order_by("external_id") assert external_projects[0].external_id == "1" assert external_projects[0].resolved_status == "ResolvedStatus1" assert external_projects[0].unresolved_status == "UnresolvedStatus1" assert external_projects[1].external_id == "2" assert external_projects[1].resolved_status == "ResolvedStatus2" assert external_projects[1].unresolved_status == "UnresolvedStatus2" assert external_projects[2].external_id == "4" assert external_projects[2].resolved_status == "ResolvedStatus4" assert external_projects[2].unresolved_status == "UnresolvedStatus4" config = OrganizationIntegration.objects.get( organization_id=org_integration.organization_id, integration_id=org_integration.integration_id, ).config assert config == {"sync_status_forward": True, "other_option": "hello"}
def test_update_organization_config(self): self.assert_installation() org_integration = OrganizationIntegration.objects.get( organization_id=self.organization.id, ) model = Integration.objects.get(provider='vsts') integration = VstsIntegration(model, self.organization.id) # test validation data = { 'sync_status_forward': { 1: { 'on_resolve': '', 'on_unresolve': 'UnresolvedStatus1', }, }, } with self.assertRaises(IntegrationError): integration.update_organization_config(data) data = { 'sync_status_forward': { 1: { 'on_resolve': 'ResolvedStatus1', 'on_unresolve': 'UnresolvedStatus1', }, 2: { 'on_resolve': 'ResolvedStatus2', 'on_unresolve': 'UnresolvedStatus2', }, 4: { 'on_resolve': 'ResolvedStatus4', 'on_unresolve': 'UnresolvedStatus4', }, }, 'other_option': 'hello', } IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=1, resolved_status='UpdateMe', unresolved_status='UpdateMe', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=2, resolved_status='ResolvedStatus2', unresolved_status='UnresolvedStatus2', ) IntegrationExternalProject.objects.create( organization_integration_id=org_integration.id, external_id=3, resolved_status='ResolvedStatus3', unresolved_status='UnresolvedStatus3', ) integration.update_organization_config(data) external_projects = IntegrationExternalProject.objects \ .all() \ .order_by('external_id') assert external_projects[0].external_id == '1' assert external_projects[0].resolved_status == 'ResolvedStatus1' assert external_projects[0].unresolved_status == 'UnresolvedStatus1' assert external_projects[1].external_id == '2' assert external_projects[1].resolved_status == 'ResolvedStatus2' assert external_projects[1].unresolved_status == 'UnresolvedStatus2' assert external_projects[2].external_id == '4' assert external_projects[2].resolved_status == 'ResolvedStatus4' assert external_projects[2].unresolved_status == 'UnresolvedStatus4' config = OrganizationIntegration.objects.get( organization_id=org_integration.organization_id, integration_id=org_integration.integration_id ).config assert config == { 'sync_status_forward': True, 'other_option': 'hello', }