Skip to content

Commit

Permalink
Fix TVDB Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Caselani committed Mar 25, 2020
1 parent 5b884a0 commit af59e23
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CouchTrackerApp/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public final class Environment {
}

private static func checkForEmptySecrets() {
guard NSClassFromString("XCTestCase") == nil else { return }

let traktClientId = Secrets.Trakt.clientId.isEmpty
let tmdbAPIKey = Secrets.TMDB.apiKey.isEmpty
let tvdbAPIKey = Secrets.TVDB.apiKey.isEmpty
Expand Down
10 changes: 9 additions & 1 deletion TVDBSwift/TVDBTokenRequestInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class TVDBTokenRequestInterceptor: RequestInterceptor {
if tvdb.hasValidToken {
done(.success(request))
} else {
doLogin(target, tvdb, request, endpoint, done)
refreshToken(target, tvdb, request, endpoint, done)
}
}
}
Expand All @@ -41,6 +41,14 @@ final class TVDBTokenRequestInterceptor: RequestInterceptor {
requestToken(tvdb, Authentication.login(apiKey: tvdb.apiKey), request, target, done)
}

private func refreshToken<T: TVDBType>(_ target: T.Type,
_ tvdb: TVDB,
_ request: URLRequest,
_: Endpoint,
_ done: @escaping MoyaProvider<T>.RequestResultClosure) {
requestToken(tvdb, Authentication.refreshToken, request, target, done)
}

private func requestToken<T: TVDBType>(_ tvdb: TVDB,
_ target: Authentication,
_ request: URLRequest,
Expand Down
2 changes: 1 addition & 1 deletion TVDBSwiftTests/TVDBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class TVDBTests: XCTestCase {
// Then
responseExpectation.fulfill()
let requestToken = response.request?.allHTTPHeaderFields!["Authorization"]
XCTAssertNotNil(requestToken)
XCTAssertEqual(requestToken, "Bearer cooltoken!")

guard let episode = try? response.map(EpisodeResponse.self).episode else {
XCTFail("Could not decode JSON")
Expand Down
4 changes: 1 addition & 3 deletions TVDBSwiftTests/TVDBTokenRequestInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ final class TVDBTokenRequestInterceptorTests: XCTestCase {
interceptor.intercept(target: Episodes.self, endpoint: endpoint) { _ in
// Then
resultExpectation.fulfill()
XCTAssertNotNil(self.tvdb.token)
XCTAssertEqual(self.tvdb.token, "cooltoken!")
}

Expand Down Expand Up @@ -73,8 +72,7 @@ final class TVDBTokenRequestInterceptorTests: XCTestCase {
interceptor.intercept(target: Episodes.self, endpoint: endpoint) { _ in
// Then
resultExpectation.fulfill()
XCTAssertNotNil(self.tvdb.token)
XCTAssertEqual(self.tvdb.token, "cooltoken!")
XCTAssertEqual(self.tvdb.token, "cool_new_token!")
}

wait(for: [resultExpectation], timeout: 1)
Expand Down

0 comments on commit af59e23

Please sign in to comment.