Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ jobs:

- name: swift test
run: swift test
env:
STACKNUDGE_TESTS_NO_KEYCHAIN: "1"

# notify.sh is the hook side, which swift test can't reach. Runs here
# rather than in the shell job because the panel post it asserts on is
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test:
echo " sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"; \
exit 1; \
fi
@swift test
@STACKNUDGE_TESTS_NO_KEYCHAIN=1 swift test

# Drive notify.sh against a fake panel socket under a throwaway $HOME, so the
# hook side gets covered too. macOS only; a no-op elsewhere. Needs no Xcode.
Expand All @@ -77,7 +77,7 @@ typecheck-tests:
# filters to matching Class.method.
.PHONY: test-without-xcode
test-without-xcode:
@./scripts/run-tests-without-xcode.sh $(NAME)
@STACKNUDGE_TESTS_NO_KEYCHAIN=1 ./scripts/run-tests-without-xcode.sh $(NAME)

# One-shot dev cycle: rebuild, reinstall the app, refresh notify.sh in
# ~/.stack-nudge so hook-side changes propagate, kickstart the daemon.
Expand Down
1 change: 1 addition & 0 deletions Tests/StackNudgePanelCoreTests/IntegrationPageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ final class IntegrationPageTests: XCTestCase {
private func navCheckingGh(_ answer: Bool, asked: @escaping () -> Void) -> PanelNav {
let nav = PanelNav()
nav.checkGitHubCLILogin = { asked(); return answer }
nav.githubToken = { nil }
nav.runOffMain = { $0() }
nav.runOnMain = { $0() }
return nav
Expand Down
3 changes: 3 additions & 0 deletions panel/GitHubAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ enum GitHubAuth {
// MARK: - Keychain

static func token() -> String? {
// A test binary reading the real item makes macOS ask for the user's password; tests stub it.
precondition(ProcessInfo.processInfo.environment["STACKNUDGE_TESTS_NO_KEYCHAIN"] == nil,
"GitHubAuth.token reached the Keychain from a test")
var item: CFTypeRef?
let status = SecItemCopyMatching(baseQuery(returningData: true) as CFDictionary, &item)
guard status == errSecSuccess, let data = item as? Data,
Expand Down
11 changes: 8 additions & 3 deletions panel/PanelNav.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,11 @@ final class PanelNav: ObservableObject {
}
}

// The Keychain readers, swapped out by tests: a fresh test binary reading
// the real items makes macOS ask the user for their password.
var githubToken: () -> String? = GitHubAuth.token
var slackBotToken: () -> String? = SlackCredentials.botToken

func loadFromConfig() {
let config = ConfigFile.read()
hotkeyDisplay = config["STACKNUDGE_PANEL_HOTKEY"] ?? "cmd+opt+n"
Expand Down Expand Up @@ -2008,7 +2013,7 @@ final class PanelNav: ObservableObject {
githubActivityEnabled = ConfigFile.bool(config, "STACKNUDGE_GITHUB_ACTIVITY", default: false)
hideShippedTickets = ConfigFile.bool(config, "STACKNUDGE_HIDE_SHIPPED", default: false)
githubUseGH = ConfigFile.bool(config, "STACKNUDGE_GITHUB_USE_GH", default: true)
githubSignedIn = githubUsesCLILogin || GitHubAuth.token() != nil
githubSignedIn = githubUsesCLILogin || githubToken() != nil
refreshGitHubCLILogin()
quotaTrackingEnabled = ConfigFile.bool(config, "STACKNUDGE_QUOTA_TRACKING", default: true)
quotaAlertsEnabled = ConfigFile.bool(config, "STACKNUDGE_QUOTA_ALERTS", default: true)
Expand Down Expand Up @@ -2036,7 +2041,7 @@ final class PanelNav: ObservableObject {
let rawSlackIdle = Int(config["STACKNUDGE_SLACK_IDLE_MIN"] ?? "") ?? 5
slackIdleMinutes = SlackDelivery.idleMinuteOptions
.min(by: { abs($0 - rawSlackIdle) < abs($1 - rawSlackIdle) }) ?? 5
slackTokenPresent = SlackCredentials.botToken() != nil
slackTokenPresent = slackBotToken() != nil
let memberID = config["STACKNUDGE_SLACK_MEMBER_ID"]?
.trimmingCharacters(in: .whitespaces) ?? ""
slackMemberID = memberID.isEmpty ? nil : memberID
Expand Down Expand Up @@ -2497,7 +2502,7 @@ final class PanelNav: ObservableObject {

private func applyGitHubCLILogin(_ loggedIn: Bool) {
if githubUsesCLILogin != loggedIn { githubUsesCLILogin = loggedIn }
let signedIn = loggedIn || GitHubAuth.token() != nil
let signedIn = loggedIn || githubToken() != nil
if githubSignedIn != signedIn { githubSignedIn = signedIn }
}

Expand Down
3 changes: 3 additions & 0 deletions panel/SlackCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ enum SlackCredentials {
// MARK: - Keychain

static func botToken() -> String? {
// A test binary reading the real item makes macOS ask for the user's password; tests stub it.
precondition(ProcessInfo.processInfo.environment["STACKNUDGE_TESTS_NO_KEYCHAIN"] == nil,
"SlackCredentials.botToken reached the Keychain from a test")
var item: CFTypeRef?
let status = SecItemCopyMatching(baseQuery(returningData: true) as CFDictionary, &item)
guard status == errSecSuccess, let data = item as? Data,
Expand Down
Loading