11# Copyright 2026 Therp BV <https://therp.nl>.
22# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
3+ from mock import MagicMock , patch
34
45from odoo .tests .common import SavepointCase
56
7+ OUTLOOK_MIXIN = "odoo.addons.microsoft_outlook.models.microsoft_outlook_mixin"
8+
69
710class TestIrMailServer (SavepointCase ):
8- def test_compute_is_microsoft_outlook_configured (self ):
9- MailServer = self .env ["ir.mail_server" ]
11+ @classmethod
12+ def setUpClass (cls ):
13+ super (TestIrMailServer , cls ).setUpClass ()
14+ cls .MailServer = cls .env ["ir.mail_server" ]
15+ cls .outlook_server = cls .MailServer .create (
16+ {
17+ "name" : "Test Outlook Server" ,
18+ "smtp_host" : "smtp.outlook.com" ,
19+ "smtp_port" : 587 ,
20+ "smtp_encryption" : "starttls" ,
21+ "smtp_user" : "user1@somemail.com" ,
22+ "use_microsoft_outlook_service" : True ,
23+ "microsoft_outlook_client_identifier" : "test_client_id" ,
24+ "microsoft_outlook_client_secret" : "test_secret" ,
25+ }
26+ )
27+
28+ def test_multi_outlook_configured (self ):
29+ # Now test the outlook server, with client id and secret on the server record.
30+ self .assertTrue (self .outlook_server .is_microsoft_outlook_configured )
31+ self .assertIn (
32+ self .outlook_server .microsoft_outlook_client_identifier ,
33+ self .outlook_server .microsoft_outlook_uri ,
34+ )
35+
36+ def test_outlook_not_configured (self ):
1037 # Server not using outlook should not have outlook configured.
11- example_server = MailServer .create (
38+ example_server = self . MailServer .create (
1239 {
1340 "name" : "Test Outlook Server" ,
1441 "smtp_host" : "smtp.example.com" ,
@@ -22,21 +49,43 @@ def test_compute_is_microsoft_outlook_configured(self):
2249 )
2350 self .assertFalse (example_server .is_microsoft_outlook_configured )
2451 self .assertFalse (example_server .microsoft_outlook_uri )
25- # Now test an outlook server, with client id and secret on the server record.
26- outlook_server = MailServer .create (
52+
53+ def test_system_outlook_configured (self ):
54+ # Now test an outlook server, getting client id and secret from system parms.
55+ self .env ["res.config.settings" ].create (
2756 {
28- "name" : "Test Outlook Server" ,
57+ "microsoft_outlook_client_identifier" : "test_system_client_id" ,
58+ "microsoft_outlook_client_secret" : "test_system_secret" ,
59+ }
60+ ).set_values ()
61+ system_outlook_server = self .MailServer .create (
62+ {
63+ "name" : "Test System Outlook Server" ,
2964 "smtp_host" : "smtp.outlook.com" ,
3065 "smtp_port" : 587 ,
3166 "smtp_encryption" : "starttls" ,
32- "smtp_user" : "user1@somemail .com" ,
67+ "smtp_user" : "user1@example .com" ,
3368 "use_microsoft_outlook_service" : True ,
34- "microsoft_outlook_client_identifier" : "test_client_id" ,
35- "microsoft_outlook_client_secret" : "test_secret" ,
69+ "microsoft_outlook_client_identifier" : False ,
70+ "microsoft_outlook_client_secret" : False ,
3671 }
3772 )
38- self .assertTrue (outlook_server .is_microsoft_outlook_configured )
73+ self .assertTrue (system_outlook_server .is_microsoft_outlook_configured )
3974 self .assertIn (
40- outlook_server . microsoft_outlook_client_identifier ,
41- outlook_server .microsoft_outlook_uri ,
75+ "test_system_client_id" ,
76+ system_outlook_server .microsoft_outlook_uri ,
4277 )
78+
79+ @patch (OUTLOOK_MIXIN + ".requests" )
80+ def test_fetch_outlook_refresh_token (self , mock_request ):
81+ # mock the response
82+ mock_response = MagicMock ()
83+ mock_response .status_code = 200
84+ mock_response .json .return_value = {
85+ "refresh_token" : "abc" ,
86+ "access_token" : "xyz" ,
87+ "expires_in" : 90 ,
88+ }
89+ mock_request .get .return_value = mock_response
90+ result = self .outlook_server ._fetch_outlook_refresh_token ("dummy_code" )
91+ self .assertEqual (result ["reresh_token" ], "abc" )
0 commit comments