Skip to content

Commit 2410ba7

Browse files
chore: fix aswebauthenticationsession issue for automated tests
1 parent 86bdfd3 commit 2410ba7

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

Source/Immutable/Private/Immutable/Mac/ImmutableMac.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
#include "Editor.h"
88
#endif
99

10+
#import <AppKit/NSWorkspace.h>
11+
1012
ASWebAuthenticationSession *_authSession;
1113

14+
// Global variable to track if we're in automation mode
15+
static BOOL g_isInAutomationMode = NO;
16+
static NSString* _pendingRedirectScheme = nil;
17+
1218
@implementation ImmutableMac
1319

1420
- (instancetype)init {
@@ -67,6 +73,13 @@ ASWebAuthenticationSession *_authSession;
6773
}
6874

6975
- (void)launchUrl:(const char *)url forRedirectUri:(const char *)redirectUri {
76+
// For automation, use the browser-based method
77+
if (g_isInAutomationMode) {
78+
NSLog(@"Using automation mode for authentication");
79+
[self launchUrlInBrowser:url forRedirectUri:redirectUri forAutomation:YES];
80+
return;
81+
}
82+
7083
if (@available(macOS 10.15, *)) {
7184
NSURL *URL =
7285
[NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
@@ -104,9 +117,56 @@ ASWebAuthenticationSession *_authSession;
104117
}
105118
}
106119

120+
- (void)launchUrlInBrowser:(const char *)url forRedirectUri:(const char *)redirectUri forAutomation:(BOOL)forAutomation {
121+
// Set automation mode
122+
g_isInAutomationMode = forAutomation;
123+
NSLog(@"Setting automation mode: %@", g_isInAutomationMode ? @"YES" : @"NO");
124+
125+
// Create URL object
126+
NSURL *URL = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
127+
128+
// Extract the callback scheme from redirect URI
129+
NSString *redirectUriString = [[NSString alloc] initWithUTF8String:redirectUri];
130+
NSString *callbackURLScheme = [[redirectUriString componentsSeparatedByString:@":"] objectAtIndex:0];
131+
132+
// Store the redirect scheme for use when handling the callback
133+
_pendingRedirectScheme = callbackURLScheme;
134+
135+
// Register for custom URL scheme notifications (will need to be implemented in AppDelegate)
136+
[[NSNotificationCenter defaultCenter] addObserverForName:@"ImmutableCustomURLSchemeCallback"
137+
object:nil
138+
queue:[NSOperationQueue mainQueue]
139+
usingBlock:^(NSNotification *notification) {
140+
if (notification.userInfo) {
141+
NSString *callbackURLString = notification.userInfo[@"url"];
142+
if (callbackURLString) {
143+
UImmutablePassport* passport = [ImmutableMac getPassport];
144+
if (passport) {
145+
passport->HandleDeepLink(callbackURLString);
146+
}
147+
}
148+
}
149+
}];
150+
151+
// Open URL in default browser
152+
[[NSWorkspace sharedWorkspace] openURL:URL];
153+
NSLog(@"Opened URL in browser for automation: %@", URL);
154+
}
155+
107156
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:
108157
(ASWebAuthenticationSession *)session API_AVAILABLE(macos(10.15)) {
109158
return [[[NSApplication sharedApplication] windows] firstObject];
110159
}
111160

161+
// Helper method to enable automation mode
162+
+ (void)setAutomationMode:(BOOL)enabled {
163+
g_isInAutomationMode = enabled;
164+
NSLog(@"setAutomationMode called with: %@", enabled ? @"YES" : @"NO");
165+
}
166+
167+
// Helper method to check if we're in automation mode
168+
+ (BOOL)isInAutomationMode {
169+
return g_isInAutomationMode;
170+
}
171+
112172
@end

Source/Immutable/Private/Immutable/Mac/ImmutableMac.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
: NSObject <ASWebAuthenticationPresentationContextProviding>
88
+ (ImmutableMac *)instance;
99
- (void)launchUrl:(const char *)url forRedirectUri:(const char *)redirectUri;
10+
- (void)launchUrlInBrowser:(const char *)url forRedirectUri:(const char *)redirectUri forAutomation:(BOOL)forAutomation;
11+
12+
// Helper methods for automation
13+
+ (void)setAutomationMode:(BOOL)enabled;
14+
+ (BOOL)isInAutomationMode;
1015
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Immutable/Mac/ImmutableMacHelper.h"
2+
3+
#if PLATFORM_MAC
4+
#import "ImmutableMac.h"
5+
#endif
6+
7+
void FImmutableMacHelper::SetAutomationMode(bool bEnabled)
8+
{
9+
#if PLATFORM_MAC
10+
[ImmutableMac setAutomationMode:bEnabled ? YES : NO];
11+
#endif
12+
}
13+
14+
bool FImmutableMacHelper::IsInAutomationMode()
15+
{
16+
#if PLATFORM_MAC
17+
return [ImmutableMac isInAutomationMode] == YES;
18+
#else
19+
return false;
20+
#endif
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include "CoreMinimal.h"
4+
5+
/**
6+
* Helper class for interacting with the Immutable Mac implementation for automation purposes
7+
*/
8+
class IMMUTABLE_API FImmutableMacHelper
9+
{
10+
public:
11+
/**
12+
* Enable or disable automation mode for authentication
13+
* In automation mode, authentication will use the system browser with a custom URI scheme
14+
* instead of ASWebAuthenticationSession
15+
*
16+
* @param bEnabled Whether automation mode should be enabled
17+
*/
18+
static void SetAutomationMode(bool bEnabled);
19+
20+
/**
21+
* Check if automation mode is currently enabled
22+
*
23+
* @return True if automation mode is enabled, false otherwise
24+
*/
25+
static bool IsInAutomationMode();
26+
};

0 commit comments

Comments
 (0)