NYTAuthServiceTests.swift (2853B)
1 import Foundation 2 import Testing 3 4 @testable import Crossmate 5 6 @Suite("NYT auth service") 7 struct NYTAuthServiceTests { 8 @Test("Maps temporary keychain failures to unknown session state") 9 func mapsTemporaryKeychainFailuresToUnknownState() { 10 let state = NYTAuthService.sessionState(forCookieLoadResult: .temporarilyUnavailable) 11 12 #expect(state == .unknown) 13 } 14 15 @Test("Maps missing cookie to signed out session state") 16 func mapsMissingCookieToSignedOutState() { 17 let state = NYTAuthService.sessionState(forCookieLoadResult: .missing) 18 19 #expect(state == .signedOut) 20 } 21 22 @Test("Maps available cookie to signed in session state") 23 func mapsAvailableCookieToSignedInState() { 24 let state = NYTAuthService.sessionState( 25 forCookieLoadResult: .available("cookie"), 26 email: "[email protected]" 27 ) 28 29 #expect(state == .signedIn(email: "[email protected]")) 30 } 31 32 @Test("Extracts email from GraphQL profile") 33 func extractsEmailFromGraphQLProfile() throws { 34 let data = Data(""" 35 { 36 "data": { 37 "user": { 38 "profile": { 39 "email": "[email protected]" 40 } 41 } 42 } 43 } 44 """.utf8) 45 46 #expect(NYTAuthService.extractEmail(from: data) == "[email protected]") 47 } 48 49 @Test("Ignores emails outside GraphQL profile field") 50 func ignoresEmailsOutsideGraphQLProfileField() throws { 51 let data = Data(#"<html><body>Signed in as [email protected]</body></html>"#.utf8) 52 53 #expect(NYTAuthService.extractEmail(from: data) == nil) 54 } 55 56 @Test("Returns nil when no email is present") 57 func returnsNilWhenNoEmailIsPresent() throws { 58 let data = Data(#"{"data":{"name":"NYT User"}}"#.utf8) 59 60 #expect(NYTAuthService.extractEmail(from: data) == nil) 61 } 62 63 @Test("Extracts GraphQL account configuration from account page HTML") 64 func extractsGraphQLAccountConfiguration() throws { 65 let html = #""" 66 <script> 67 window.__preloadedData = { 68 "config": { 69 "gqlUrlClient": "https:\u002F\u002Fsamizdat-graphql.nytimes.com\u002Fgraphql\u002Fv2", 70 "gqlRequestHeaders": { 71 "nyt-app-type": "project-vi", 72 "nyt-app-version": "0.0.5", 73 "nyt-token": "abc\u002F123" 74 } 75 } 76 } 77 </script> 78 """# 79 80 let configuration = NYTAuthService.extractAccountGraphQLConfiguration(from: html) 81 82 #expect(configuration?.url.absoluteString == "https://samizdat-graphql.nytimes.com/graphql/v2") 83 #expect(configuration?.headers["nyt-app-type"] == "project-vi") 84 #expect(configuration?.headers["nyt-app-version"] == "0.0.5") 85 #expect(configuration?.headers["nyt-token"] == "abc/123") 86 } 87 }