lundi 23 avril 2018

Setup authentication headers within a test using Quick

I'm writing tests for my application with usage of Quick and Nimble. I've arrived at the part of the tests that require authentication. My app works in a manner that there's a singleton named AuthenticationManager that has methods for logging in etc. This class also holds the current authenticationToken:

public var token: String?

public var headers: HTTPHeaders {
    guard let token = token else {
        return ["Accept": "application/json"]
    }

    return [
        "Authorization": "Bearer \(token)",
        "Accept": "application/json"
    ]
}

This works in actual usage, however, when this runs with testing, it seems that the value of the token variable resets when my test starts. I tried making the login call in the setUp() method of my QuickSpec file, this however, didn't work. I also tried making te login call within the it statement for my test, which also didn't work. This is my code as is right now:

override func spec() {

    describe("When a user validates their current password") {
        describe("And the password is correct") {
            it("should validate the user correctly") {
                AuthenticationManager.shared.login(username: "test@example.com", password: "secret") { _ in }
                expect(Validator.validateCurrentPassword("secret")).to(equal(true))
            }
        }
    }
}

I've checked, and the login details are correct, but there's never a token available when the test runs... Does anyone have any experience with this?

Aucun commentaire:

Enregistrer un commentaire