Skip to content

Commit 6b8d63f

Browse files
authored
Merge pull request #17 from itk-dev-rpa/feature/revert_bad_revert
Feature/revert bad revert
2 parents 79e1a48 + 8fe82ba commit 6b8d63f

File tree

4 files changed

+68
-53
lines changed

4 files changed

+68
-53
lines changed

ITK_dev_shared_components/SAP/opret_kundekontakt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""This module provides a single function to conviniently peform the action 'opret-kundekontaker' in SAP."""
1+
"""This module provides a single function to conveniently perform the action 'opret-kundekontaker' in SAP."""
22

33
from typing import Literal
44
import win32clipboard
@@ -11,7 +11,7 @@ def opret_kundekontakter(session, fp:str, aftaler:list[str] | None,
1111
"""Creates a kundekontakt on the given FP and aftaler.
1212
1313
Args:
14-
session (COM Object): The SAP session to peform the action.
14+
session (COM Object): The SAP session to preform the action.
1515
fp (str): The forretningspartner number.
1616
aftaler (list[str] | None): A list of aftaler to put the kundekontakt on. If empty or None the kundekontakt will be created on fp-level.
1717
art (str): The art of the kundekontakt.

ITK_dev_shared_components/graph/authentication.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,7 @@
33

44
import msal
55

6-
def authorize_by_username_password(username: str, password: str, *, client_id: str, tenant_id: str) -> GraphAccess:
7-
"""Get a bearer token for the given user.
8-
This is used in most other Graph API calls.
9-
10-
Args:
11-
username: The username of the user (email address).
12-
password: The password of the user.
13-
client_id: The Graph API client id in 8-4-4-12 format.
14-
tenant_id: The Graph API tenant id in 8-4-4-12 format.
15-
16-
Returns:
17-
GraphAccess: The GraphAccess object used to authorize Graph access.
18-
"""
19-
authority = f"https://login.microsoftonline.com/{tenant_id}"
20-
scopes = ["https://graph.microsoft.com/.default"]
21-
22-
app = msal.PublicClientApplication(client_id, authority=authority)
23-
app.acquire_token_by_username_password(username, password, scopes)
24-
25-
graph_access = GraphAccess(app, scopes)
26-
27-
# Test connection
28-
graph_access.get_access_token()
29-
30-
return graph_access
31-
32-
6+
# pylint: disable-next=too-few-public-methods
337
class GraphAccess:
348
"""An object that handles access to the Graph api.
359
This object should not be created directly but instead
@@ -41,7 +15,7 @@ def __init__(self, app: msal.PublicClientApplication, scopes: list[str]) -> str:
4115

4216
def get_access_token(self):
4317
"""Get the access token to Graph.
44-
This function automatically reuses an existing token
18+
This function automatically reuses an existing token
4519
or refreshes an expired one.
4620
4721
Raises:
@@ -60,3 +34,30 @@ def get_access_token(self):
6034
raise RuntimeError(f"Token could not be acquired. {token['error_description']}")
6135

6236
raise RuntimeError("Something went wrong. No error description was returned from Graph.")
37+
38+
39+
def authorize_by_username_password(username: str, password: str, *, client_id: str, tenant_id: str) -> GraphAccess:
40+
"""Get a bearer token for the given user.
41+
This is used in most other Graph API calls.
42+
43+
Args:
44+
username: The username of the user (email address).
45+
password: The password of the user.
46+
client_id: The Graph API client id in 8-4-4-12 format.
47+
tenant_id: The Graph API tenant id in 8-4-4-12 format.
48+
49+
Returns:
50+
GraphAccess: The GraphAccess object used to authorize Graph access.
51+
"""
52+
authority = f"https://login.microsoftonline.com/{tenant_id}"
53+
scopes = ["https://graph.microsoft.com/.default"]
54+
55+
app = msal.PublicClientApplication(client_id, authority=authority)
56+
app.acquire_token_by_username_password(username, password, scopes)
57+
58+
graph_access = GraphAccess(app, scopes)
59+
60+
# Test connection
61+
graph_access.get_access_token()
62+
63+
return graph_access

ITK_dev_shared_components/graph/mail.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
@dataclass
13+
# pylint: disable-next=too-many-instance-attributes
1314
class Email:
1415
"""A class representing an email."""
1516
user: str
@@ -316,26 +317,3 @@ def _get_request(endpoint: str, graph_access: GraphAccess) -> requests.models.Re
316317
response.raise_for_status()
317318

318319
return response
319-
320-
321-
if __name__ == '__main__':
322-
import os, json, authentication
323-
def main():
324-
credentials = json.loads(os.environ['GraphAPI'])
325-
client_id = credentials['client_id']
326-
tenant_id = credentials['tenant_id']
327-
username = credentials['username']
328-
password = credentials['password']
329-
330-
graph_access = authentication.authorize_by_username_password(username, password, client_id=client_id, tenant_id=tenant_id)
331-
332-
emails = get_emails_from_folder("itk-rpa@mkb.aarhus.dk", "Indbakke/Graph Test/Undermappe", graph_access)
333-
email = emails[0]
334-
335-
print(repr(email.get_text()))
336-
337-
# move_email(email, "Indbakke/Graph Test/Undermappe2", graph_access)
338-
339-
# list_email_attachments(email, graph_access)
340-
341-
main()

tests/test_opret_kundekontakt.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Test relating to the module SAP.opret_kundekontakt."""
2+
3+
import unittest
4+
import os
5+
from ITK_dev_shared_components.SAP import sap_login, multi_session, opret_kundekontakt
6+
7+
class TestOpretKundekontakt(unittest.TestCase):
8+
"""Test relating to the module SAP.opret_kundekontakt."""
9+
def setUp(self):
10+
sap_login.kill_sap()
11+
user, password = os.environ['SAP Login'].split(';')
12+
sap_login.login_using_cli(user, password)
13+
14+
def tearDown(self):
15+
sap_login.kill_sap()
16+
17+
18+
def test_opret_kundekontakt(self):
19+
"""Test the function opret_kundekontakter."""
20+
fp = "25564617"
21+
aftaler = ("2544577", "1990437", "1473781")
22+
23+
session = multi_session.get_all_SAP_sessions()[0]
24+
25+
# Test with 3 aftaler
26+
opret_kundekontakt.opret_kundekontakter(session, fp, aftaler, 'Orientering', "Test 1")
27+
28+
# Test with 1 aftale
29+
opret_kundekontakt.opret_kundekontakter(session, fp, aftaler[0:1], 'Automatisk', "Test 2")
30+
31+
# Test with 0 aftaler
32+
opret_kundekontakt.opret_kundekontakter(session, fp, None, 'Returpost', "Test 3")
33+
34+
35+
if __name__ == '__main__':
36+
unittest.main()

0 commit comments

Comments
 (0)