← All repositories

crossmate

A collaborative crossword app for iPhone and iPad

git clone https://code.inqk.net/crossmate.git


Tests/Unit/NYTAuthServiceTests.swift (4051B)

1import Foundation
2import Testing
3
4@testable import Crossmate
5
6@Suite("NYT auth service")
7struct 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
88    @Test("Rejects non-HTTPS GraphQL endpoint in account page HTML")
89    func rejectsNonHTTPSGraphQLEndpoint() throws {
90        let html = #"""
91        <script>
92        window.__preloadedData = {
93          "config": {
94            "gqlUrlClient": "http://samizdat-graphql.nytimes.com/graphql/v2",
95            "gqlRequestHeaders": {
96              "nyt-app-type": "project-vi",
97              "nyt-app-version": "0.0.5",
98              "nyt-token": "abc/123"
99            }
100          }
101        }
102        </script>
103        """#
104
105        #expect(NYTAuthService.extractAccountGraphQLConfiguration(from: html) == nil)
106    }
107
108    @Test("Rejects schemeless GraphQL endpoint in account page HTML")
109    func rejectsSchemelessGraphQLEndpoint() throws {
110        let html = #"""
111        <script>
112        window.__preloadedData = {
113          "config": {
114            "gqlUrlClient": "/graphql/v2",
115            "gqlRequestHeaders": {
116              "nyt-app-type": "project-vi",
117              "nyt-app-version": "0.0.5",
118              "nyt-token": "abc/123"
119            }
120          }
121        }
122        </script>
123        """#
124
125        #expect(NYTAuthService.extractAccountGraphQLConfiguration(from: html) == nil)
126    }
127}