Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct Key: Codable, Equatable {

public let uid: String
public let name: String?
public let description: String
public let description: String?
public let key: String
public let actions: [String]
public let indexes: [String]
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
`KeyParams` contains all the parameters to create an API key.
*/
public struct KeyParams: Codable, Equatable {
public let description: String
public var description: String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, why are some var and some let? We cant change actions ? Is this because of updateKey ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct!

public var name: String?
public var uid: String?
public let actions: [String]
Expand Down
91 changes: 69 additions & 22 deletions Tests/MeiliSearchIntegrationTests/KeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,93 @@ import Foundation
// swiftlint:disable force_try
class KeysTests: XCTestCase {
private var client: MeiliSearch!
private var key: String = ""
private var session: URLSessionProtocol!

// MARK: Setup

override func setUp() {
super.setUp()

session = URLSession(configuration: .ephemeral)
client = try! MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey", session: session)
let keyExpectation = XCTestExpectation(description: "Get all keys")

self.client.getKeys { result in
// remove all keys to keep a clean state
self.client.getKeys(params: KeysQuery(limit: 100, offset: 0)) { result in
switch result {
case .success(let keys):
if keys.results.count > 0 {
let key = keys.results.first
if let firstKey: Key = key {
self.key = firstKey.key
keys.results.forEach {
self.client.deleteKey(key: $0.uid) { result in
switch result {
case .success:
()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the same as keyExpectation.fulfill() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we define a switch, we should provide an exhaustive list of possibilities, such as success and failure. The problem is that Swift requires me to provide at least a statement when defining a case. Since this method is meant to be a setup and I'm not interested in the success, I could provide the () empty function, but I would like to know if there is another way to handle that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you are not interested in the success is .fulfill() not used to stop the wait of the expectation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but that's the point, this code is written inside of the setup(), so it's not meant to be a "test" so in other words, no expectation should be needed 😬

case .failure(let error):
dump(error)
XCTFail("Failed to delete key")
}
}
} else {
XCTFail("Failed to get keys")
}
keyExpectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to get keys")
keyExpectation.fulfill()
XCTFail("Failed to retrieve and delete all keys")
}
}
self.wait(for: [keyExpectation], timeout: TESTS_TIME_OUT)
}

func testGetKeys() {
let keyExpectation = XCTestExpectation(description: "Get all keys")
let keyExpectation = XCTestExpectation(description: "Get a list of keys")

self.client.getKeys { result in
self.client.createKey(KeyParams(actions: ["*"], indexes: ["*"], expiresAt: nil)) { result in
switch result {
case .success(let keys):
XCTAssertNotEqual(keys.results.count, 0)
case .success:
self.client.getKeys { result in
switch result {
case .success(let keys):
XCTAssertNotEqual(keys.results.count, 0)
case .failure(let error):
dump(error)
XCTFail("Failed to get all keys")
}
}

keyExpectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to get all keys")
XCTFail("Failed to create a key")
keyExpectation.fulfill()
}
}

self.wait(for: [keyExpectation], timeout: TESTS_TIME_OUT)
}

func testGetKey() {
let keyExpectation = XCTestExpectation(description: "Get one key")

self.client.getKey(key: self.key) { result in
self.client.createKey(KeyParams(actions: ["*"], indexes: ["*"], expiresAt: nil)) { result in
switch result {
case .success(let key):
XCTAssertNotNil(key.description)
case .success(let createdKey):
self.client.getKey(key: createdKey.uid) { result in
switch result {
case .success(let fetchedKey):
XCTAssertEqual(fetchedKey.expiresAt, nil)
XCTAssertEqual(fetchedKey.description, createdKey.description)
XCTAssertEqual(fetchedKey.actions, createdKey.actions)
XCTAssertEqual(fetchedKey.indexes, createdKey.indexes)
XCTAssertEqual(fetchedKey.uid, createdKey.uid)
case .failure(let error):
dump(error)
XCTFail("Failed to get key by uid \(createdKey.uid)")
}
}

keyExpectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to get a key")
XCTFail("Failed to create a key")
keyExpectation.fulfill()
}
}

self.wait(for: [keyExpectation], timeout: TESTS_TIME_OUT)
}

Expand All @@ -93,6 +117,29 @@ class KeysTests: XCTestCase {
self.wait(for: [keyExpectation], timeout: TESTS_TIME_OUT)
}

func testCreateKeyWithOptionalUid() {
let keyExpectation = XCTestExpectation(description: "Create a key")
let uid = "1f05fa47-cfa6-40f7-8b80-7bd17b39f105"
let keyParams = KeyParams(uid: uid, actions: ["*"], indexes: ["*"], expiresAt: nil)

self.client.createKey(keyParams) { result in
switch result {
case .success(let key):
XCTAssertEqual(key.expiresAt, nil)
XCTAssertEqual(key.uid, uid)
XCTAssertEqual(key.actions, keyParams.actions)
XCTAssertEqual(key.indexes, keyParams.indexes)
keyExpectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to create a key")
keyExpectation.fulfill()
}
}

self.wait(for: [keyExpectation], timeout: TESTS_TIME_OUT)
}

func testCreateKeyWithExpire() {
let keyExpectation = XCTestExpectation(description: "Create a key with an expireAt value")

Expand Down