Skip to content

Commit f736cee

Browse files
Merge pull request #237 from HubSpot/feature/add_integration_tests
Feature/add integration tests
2 parents c8eb45a + a1228b4 commit f736cee

File tree

6 files changed

+325
-1
lines changed

6 files changed

+325
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: Python integration tests
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
10+
jobs:
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Set up Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: 3.x
21+
- name: Integration tests with pytest
22+
env:
23+
HUBSPOT_ACCESS_TOKEN: ${{ secrets.HUBSPOT_ACCESS_TOKEN }}
24+
HUBSPOT_DEVELOPER_API_KEY: ${{ secrets.HUBSPOT_DEVELOPER_API_KEY }}
25+
HUBSPOT_APP_ID: ${{ secrets.HUBSPOT_APP_ID }}
26+
run: make integration_test

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ $(VENV_NAME)/bin/activate: setup.py
99
@touch $(VENV_NAME)/bin/activate
1010

1111
test: venv
12-
@${VENV_NAME}/bin/python -m pytest
12+
@${VENV_NAME}/bin/python -m pytest ./tests/spec
13+
14+
integration_test: venv
15+
@${VENV_NAME}/bin/python -m pytest ./tests/integration
1316

1417
fmt: venv
1518
@${VENV_NAME}/bin/python -m black ./

tests/integration/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import hubspot
2+
import pytest
3+
import os
4+
5+
6+
@pytest.fixture
7+
def api_client():
8+
client = hubspot.Client.create(access_token=os.getenv("HUBSPOT_ACCESS_TOKEN"))
9+
10+
return client
11+
12+
13+
@pytest.fixture
14+
def api_client_by_api_key():
15+
client = hubspot.Client.create(api_key=os.getenv("HUBSPOT_DEVELOPER_API_KEY"))
16+
17+
return client

tests/integration/test_contacts.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import pytest
2+
3+
from hubspot.crm.contacts import PublicObjectSearchRequest
4+
5+
CONTACT_PROPERTIES = {
6+
"email": "[email protected]",
7+
"lastname": "Rocket",
8+
"firstname": "Racoon"
9+
}
10+
11+
12+
@pytest.fixture
13+
def create_contact(api_client):
14+
contact = api_client.crm.contacts.basic_api.create(CONTACT_PROPERTIES)
15+
16+
yield contact
17+
18+
api_client.crm.contacts.basic_api.archive(contact_id=contact.id)
19+
20+
21+
@pytest.fixture
22+
def create_company(api_client):
23+
properties = {
24+
"domain": "TestCompany.net",
25+
"name": "TestCompany",
26+
}
27+
28+
company = api_client.crm.companies.basic_api.create(properties)
29+
30+
yield company
31+
32+
api_client.crm.companies.basic_api.archive(company_id=company.id)
33+
34+
35+
def get_or_create_contact_associated_with_company(api_client, create_company):
36+
contact_data = {
37+
"associations": [
38+
{
39+
"to": {"id": create_company.id},
40+
"types": [
41+
{
42+
"associationCategory": "HUBSPOT_DEFINED",
43+
"associationTypeId": 1
44+
}
45+
]
46+
}
47+
],
48+
"properties": CONTACT_PROPERTIES
49+
}
50+
public_object_search_request = PublicObjectSearchRequest(
51+
filter_groups=[
52+
{
53+
"filters": [
54+
{
55+
"value": contact_data["properties"]["email"],
56+
"propertyName": "email",
57+
"operator": "EQ"
58+
}
59+
]
60+
}
61+
]
62+
)
63+
contact_response = api_client.crm.contacts.search_api.do_search(public_object_search_request)
64+
if contact_response.results:
65+
return contact_response.results[0]
66+
else:
67+
return api_client.crm.contacts.basic_api.create(contact_data)
68+
69+
70+
def test_create_contact_with_company_association(api_client, create_company):
71+
contact = get_or_create_contact_associated_with_company(api_client, create_company)
72+
73+
assert contact
74+
75+
76+
def test_contact__create(api_client):
77+
contact = api_client.crm.contacts.basic_api.create(CONTACT_PROPERTIES)
78+
79+
assert contact
80+
81+
api_client.crm.contacts.basic_api.archive(contact_id=contact.id)
82+
83+
84+
def test_contact__get_page(api_client):
85+
result = api_client.crm.companies.basic_api.get_page()
86+
87+
assert result
88+
89+
90+
def test_contact__get_by_id(api_client, create_contact):
91+
result = api_client.crm.contacts.basic_api.get_by_id(create_contact.id)
92+
93+
assert result
94+
95+
96+
def test_contact__get_all(api_client):
97+
result = api_client.crm.contacts.get_all()
98+
99+
assert result
100+
101+
102+
def test_contact__update(api_client, create_contact):
103+
simple_public_object_input = {
104+
"properties": {
105+
"firstname": "updated_name"
106+
}
107+
}
108+
109+
result = api_client.crm.contacts.basic_api.update(create_contact.id, simple_public_object_input)
110+
111+
assert result.properties["firstname"] == simple_public_object_input["properties"]["firstname"]
112+
113+
114+
def test_contact__archive(api_client, create_contact):
115+
result = api_client.crm.contacts.basic_api.archive(create_contact.id)
116+
117+
assert result is None
118+
119+
120+
def test_contact__do_search(api_client, create_contact):
121+
public_object_search_request = PublicObjectSearchRequest(
122+
filter_groups=[
123+
{
124+
"filters": [
125+
{
126+
"value": "[email protected]",
127+
"propertyName": "email",
128+
"operator": "EQ"
129+
}
130+
]
131+
}
132+
]
133+
)
134+
contact = api_client.crm.contacts.search_api.do_search(public_object_search_request)
135+
136+
assert contact.results

tests/integration/test_hub_db.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import pytest
2+
import os
3+
4+
from hubspot.cms.hubdb import HubDbTableV3Request
5+
from hubspot.cms.hubdb.exceptions import NotFoundException
6+
7+
8+
HUB_DB_TABLE_REQUEST = HubDbTableV3Request(
9+
name="test_table",
10+
label="Test Table",
11+
use_for_pages=True,
12+
allow_public_api_access=False,
13+
allow_child_tables=True,
14+
enable_child_table_pages=False,
15+
columns=[
16+
{
17+
"name": "text_column",
18+
"label": "Text Column",
19+
"id": "1",
20+
"type": "TEXT"
21+
}
22+
],
23+
dynamic_meta_tags={}
24+
)
25+
26+
27+
@pytest.fixture
28+
def get_or_create_table(api_client):
29+
try:
30+
table = api_client.cms.hubdb.tables_api.get_table_details(
31+
table_id_or_name=HUB_DB_TABLE_REQUEST.name
32+
)
33+
except NotFoundException:
34+
table = api_client.cms.hubdb.tables_api.create_table(HUB_DB_TABLE_REQUEST)
35+
36+
yield table
37+
38+
api_client.cms.hubdb.tables_api.archive_table(table_id_or_name=table.name)
39+
40+
41+
def test_hub_db__get_all_tables(api_client):
42+
result = api_client.cms.hubdb.tables_api.get_all_tables()
43+
44+
assert result
45+
46+
47+
def test_hub_db__create_table(api_client):
48+
table = api_client.cms.hubdb.tables_api.create_table(HUB_DB_TABLE_REQUEST)
49+
50+
assert table
51+
52+
api_client.cms.hubdb.tables_api.archive_table(table_id_or_name=table.name)
53+
54+
55+
def test_hub_db__archive_table(api_client):
56+
table = api_client.cms.hubdb.tables_api.create_table(HUB_DB_TABLE_REQUEST)
57+
58+
result = api_client.cms.hubdb.tables_api.archive_table(table_id_or_name=table.name)
59+
60+
assert result is None
61+
62+
63+
def test_hub_db__get_all_draft_tables(api_client):
64+
result = api_client.cms.hubdb.tables_api.get_all_draft_tables()
65+
66+
assert result
67+
68+
69+
def test_hub_db__get_table_details(api_client, get_or_create_table):
70+
result = api_client.cms.hubdb.tables_api.get_table_details(table_id_or_name=get_or_create_table.name)
71+
72+
assert result
73+
74+
75+
def test_hub_db__export_draft_table(api_client, get_or_create_table):
76+
result = api_client.cms.hubdb.tables_api.export_draft_table(
77+
table_id_or_name=get_or_create_table.name,
78+
format="CSV"
79+
)
80+
81+
assert result
82+
83+
os.remove(result)
84+
85+
86+
def test_hub_db__get_table_rows(api_client, get_or_create_table):
87+
result = api_client.cms.hubdb.rows_api.get_table_rows(table_id_or_name=get_or_create_table.name)
88+
89+
assert result
90+
91+
92+
def test_hub_db__unpublish_table(api_client, get_or_create_table):
93+
result = api_client.cms.hubdb.tables_api.unpublish_table(table_id_or_name=get_or_create_table.name)
94+
95+
assert result
96+
97+
98+
def test_hub_db__update_draft_table(api_client, get_or_create_table):
99+
HUB_DB_TABLE_REQUEST.label = "updated label"
100+
101+
result = api_client.cms.hubdb.tables_api.update_draft_table(
102+
table_id_or_name=get_or_create_table.id,
103+
hub_db_table_v3_request=HUB_DB_TABLE_REQUEST
104+
)
105+
106+
assert result

tests/integration/test_webhooks.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
from hubspot.webhooks import SettingsChangeRequest
4+
5+
6+
APP_ID = os.getenv("HUBSPOT_APP_ID")
7+
8+
9+
def test_webhooks__get_all(api_client_by_api_key):
10+
result = api_client_by_api_key.webhooks.settings_api.get_all(app_id=APP_ID)
11+
12+
assert result
13+
14+
15+
def test_webhooks__configure(api_client_by_api_key):
16+
throttling = {
17+
"maxConcurrentRequests": 10,
18+
"period": "SECONDLY"
19+
}
20+
settings_change_request = SettingsChangeRequest(
21+
target_url="https://www.test.com/test/target",
22+
throttling=throttling
23+
)
24+
25+
result = api_client_by_api_key.webhooks.settings_api.configure(
26+
app_id=APP_ID,
27+
settings_change_request=settings_change_request
28+
)
29+
30+
assert result
31+
32+
33+
def test_webhooks_subscriptions__get_all(api_client_by_api_key):
34+
result = api_client_by_api_key.webhooks.subscriptions_api.get_all(app_id=APP_ID)
35+
36+
assert result

0 commit comments

Comments
 (0)