//
//  TokenExemptionTests.swift
//  PadelClubTests
//
//  Created by Laurent Morvillier on 14/05/2024.
//


import XCTest
import LeStorage
@testable import PadelClub

final class TokenExemptionTests: XCTestCase {
    
    let username: String = "test"
    let password: String = "MyPass1234--"
    
    override func setUpWithError() throws {
        Store.main.synchronizationApiURL = "http://127.0.0.1:8000/roads/"
        Store.main.disconnect()
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func testClubCreation() async throws {
        
        let user = try await self.login()
        Store.main.disconnect()

        let club: Club = Club(name: "mon club 2", acronym: "MC", phone: "132", code: "456", address: "l'adresse", city: "la ville", zipCode: "13131", latitude: 13.11111, longitude: 1.121212)
        
        let c = try await Store.main.service().post(club)
        assert(c.id == club.id)
        
        do {
            _ = try await Store.main.service().put(club)
            assertionFailure("the request above should fail without an authenticated user")
        } catch {
            // good stuff
        }
        
        let _ = try await self.login()
        club.creator = user.id
        
        let uc = try await Store.main.service().put(club)
        assert(uc.creator == user.id)

    }
    
    func login() async throws -> User {
        let user: User = try await Store.main.service().login(username: self.username, password: self.password)
        return user
    }
    
}
