crossmate

A collaborative crossword app for iOS
Log | Files | Refs | LICENSE

NYTAuthServiceTests.swift (2019B)


      1 import Foundation
      2 import Testing
      3 
      4 @testable import Crossmate
      5 
      6 @Suite("NYT auth service")
      7 struct NYTAuthServiceTests {
      8     @Test("Extracts email from GraphQL profile")
      9     func extractsEmailFromGraphQLProfile() throws {
     10         let data = Data("""
     11         {
     12           "data": {
     13             "user": {
     14               "profile": {
     15                 "email": "[email protected]"
     16               }
     17             }
     18           }
     19         }
     20         """.utf8)
     21 
     22         #expect(NYTAuthService.extractEmail(from: data) == "[email protected]")
     23     }
     24 
     25     @Test("Ignores emails outside GraphQL profile field")
     26     func ignoresEmailsOutsideGraphQLProfileField() throws {
     27         let data = Data(#"<html><body>Signed in as [email protected]</body></html>"#.utf8)
     28 
     29         #expect(NYTAuthService.extractEmail(from: data) == nil)
     30     }
     31 
     32     @Test("Returns nil when no email is present")
     33     func returnsNilWhenNoEmailIsPresent() throws {
     34         let data = Data(#"{"data":{"name":"NYT User"}}"#.utf8)
     35 
     36         #expect(NYTAuthService.extractEmail(from: data) == nil)
     37     }
     38 
     39     @Test("Extracts GraphQL account configuration from account page HTML")
     40     func extractsGraphQLAccountConfiguration() throws {
     41         let html = #"""
     42         <script>
     43         window.__preloadedData = {
     44           "config": {
     45             "gqlUrlClient": "https:\u002F\u002Fsamizdat-graphql.nytimes.com\u002Fgraphql\u002Fv2",
     46             "gqlRequestHeaders": {
     47               "nyt-app-type": "project-vi",
     48               "nyt-app-version": "0.0.5",
     49               "nyt-token": "abc\u002F123"
     50             }
     51           }
     52         }
     53         </script>
     54         """#
     55 
     56         let configuration = NYTAuthService.extractAccountGraphQLConfiguration(from: html)
     57 
     58         #expect(configuration?.url.absoluteString == "https://samizdat-graphql.nytimes.com/graphql/v2")
     59         #expect(configuration?.headers["nyt-app-type"] == "project-vi")
     60         #expect(configuration?.headers["nyt-app-version"] == "0.0.5")
     61         #expect(configuration?.headers["nyt-token"] == "abc/123")
     62     }
     63 }