Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user tests #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions Sources/WriteFreely/WFClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public class WFClient {
/// - Parameters:
/// - token: The access token for the user to fetch.
/// - completion: A handler for the `Data` object returned on success, or `Error` on failure.
public func getUserData(token: String? = nil, completion: @escaping (Result<Data, Error>) -> Void) {
public func getUserData(token: String? = nil, completion: @escaping (Result<WFUser, Error>) -> Void) {
if token == nil && user == nil {
completion(.failure(WFError.couldNotComplete))
return
Expand All @@ -891,10 +891,17 @@ public class WFClient {
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(tokenToVerify, forHTTPHeaderField: "Authorization")

get(with: request) { result in
get(with: request) { [weak self] result in
guard let self = self else { return }

switch result {
case .success(let data):
completion(.success(data))
do {
let user = try self.decoder.decode(WFUser.self, from: data)
completion(.success(user))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/WriteFreelyTests/Resources/forbidden_403.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 403,
"error_msg": "Forbidden"
}
4 changes: 4 additions & 0 deletions Tests/WriteFreelyTests/Resources/incorrect_password_401.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 401,
"error_msg": "Incorrect password."
}
4 changes: 4 additions & 0 deletions Tests/WriteFreelyTests/Resources/invalid_token_401.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 401,
"error_msg": "Invalid access token."
}
4 changes: 4 additions & 0 deletions Tests/WriteFreelyTests/Resources/invalid_token_404.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 404,
"error_msg": "Token is invalid."
}
25 changes: 25 additions & 0 deletions Tests/WriteFreelyTests/Resources/test_fetch_user_collections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"code": 200,
"data": [
{
"alias": "matt",
"title": "Matt",
"description": "My great blog!",
"style_sheet": "",
"public": true,
"views": 46,
"email": "[email protected]",
"url": "https://write.as/matt/"
},
{
"alias": "new-blog",
"title": "Test Blog",
"description": "Another great blog!",
"style_sheet": "",
"public": false,
"views": 0,
"email": "[email protected]",
"url": "https://write.as/new-blog/"
}
]
}
4 changes: 4 additions & 0 deletions Tests/WriteFreelyTests/Resources/test_logout_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"code": 204,
"data": {}
}
11 changes: 11 additions & 0 deletions Tests/WriteFreelyTests/Resources/test_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"code": 200,
"data": {
"access_token": "00000000-0000-0000-0000-000000000000",
"user": {
"username": "matt",
"email": "[email protected]",
"created": "2015-02-03T02:41:19Z"
}
}
}
4 changes: 2 additions & 2 deletions Tests/WriteFreelyTests/WriteFreelyClientCollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class WriteFreelyClientCollectionTests: XCTestCase {
}

func testCreateCollection_WithInvalidCollectionData_ReturnsBadRequestError() {
guard let _ = try? session.setData(resource: "error_collection_400", fileExt: "json", for: self) else {
guard let _ = try? session.setData(resource: "bad_request_400", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}
Expand Down Expand Up @@ -105,7 +105,7 @@ final class WriteFreelyClientCollectionTests: XCTestCase {
}

func testGetCollection_WithInvalidCollectionData_ReturnsInvalidDataError() {
guard let _ = try? session.setData(resource: "error_collection_400", fileExt: "json", for: self) else {
guard let _ = try? session.setData(resource: "bad_request_400", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}
Expand Down
247 changes: 247 additions & 0 deletions Tests/WriteFreelyTests/WriteFreelyClientUserTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
import XCTest
@testable import WriteFreely

final class WriteFreelyClientUserTests: XCTestCase {
static var allTests = [
("testLogin_WithValidCredentials_SetsCurrentUser", testLogin_WithValidCredentials_SetsCurrentUser),
("testLogin_WithInvalidCredentials_ReturnsUnauthorized", testLogin_WithInvalidCredentials_ReturnsUnauthorized),
("testLogout_WithValidToken_CompletesSuccessfully", testLogout_WithValidToken_CompletesSuccessfully),
("testLogout_WithoutAccessToken_ReturnsBadRequestError", testLogout_WithoutAccessToken_ReturnsBadRequestError),
("testLogout_WithInvalidToken_ReturnsNotFoundError", testLogout_WithInvalidToken_ReturnsNotFoundError),
("testGetUserData_WithValidToken_FetchesTestUser", testGetUserData_WithValidToken_FetchesTestUser),
("testGetUserData_WithInvalidToken_ReturnsUnauthorized", testGetUserData_WithInvalidToken_ReturnsUnauthorized),
]

var client: WFClient!
var session: MockURLSession!
var instanceURL: URL!

let decoder = JSONDecoder()

override func setUpWithError() throws {
super.setUp()
session = MockURLSession()
instanceURL = URL(string: "https://write.as/")
client = WFClient(for: instanceURL, with: session)
}

func testLogin_WithValidCredentials_SetsCurrentUser() {
guard let _ = try? session.setData(resource: "test_user", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 200

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: "2015-02-03T02:41:19Z")
let expectedUser = WFUser(
token: "00000000-0000-0000-0000-000000000000",
username: "matt",
email: "[email protected]",
createdDate: date
)

client.login(username: "[email protected]", password: "goodpassword", completion: { result in
switch result {
case .success(let user):
XCTAssertEqual(user.username, expectedUser.username)
XCTAssertEqual(user.token, expectedUser.token)
XCTAssertEqual(user.createdDate, expectedUser.createdDate)
XCTAssertEqual(user.email, expectedUser.email)
case .failure(let error):
XCTFail(error.localizedDescription)
}
})
}

func testLogin_WithInvalidCredentials_ReturnsUnauthorized() {
guard let _ = try? session.setData(resource: "incorrect_password_401", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 401

client.login(username: "[email protected]", password: "badpassword", completion: { result in
switch result {
case .success:
XCTFail("Logged in successfully with invalid credentials")
case .failure(let error):
XCTAssertEqual(error as? WFError, WFError.unauthorized)
}
})
}

func testLogout_WithValidToken_CompletesSuccessfully() {
guard let _ = try? session.setData(resource: "test_logout_user", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 204

client.logout(token: "00000000-0000-0000-0000-000000000000", completion: { result in
switch result {
case .success(let success):
XCTAssertTrue(success, "User logout should return true")
case .failure(let error):
XCTFail(error.localizedDescription)
}
})
}

func testLogout_WithoutAccessToken_ReturnsBadRequestError() {
guard let _ = try? session.setData(resource: "bad_request_400", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 400

client.logout(token: "", completion: { result in
switch result {
case .success:
XCTFail("Logged out successfully without passing access token")
case .failure(let error):
XCTAssertEqual(error as? WFError, WFError.badRequest)
}
})
}

func testLogout_WithInvalidToken_ReturnsNotFoundError() {
guard let _ = try? session.setData(resource: "invalid_token_404", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 404

client.logout(token: "1234", completion: { result in
switch result {
case .success:
XCTFail("Logged out successfully without passing access token")
case .failure(let error):
XCTAssertEqual(error as? WFError, WFError.notFound)
}
})
}

func testGetUserData_WithValidToken_FetchesTestUser() {
guard let _ = try? session.setData(resource: "test_user", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 200
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: "2015-02-03T02:41:19Z")
let expectedUser = WFUser(
token: "00000000-0000-0000-0000-000000000000",
username: "matt",
email: "[email protected]",
createdDate: date
)

client.getUserData(token: "00000000-0000-0000-0000-000000000000", completion: { result in
switch result {
case .success(let user):
XCTAssertEqual(expectedUser.username, user.username)
XCTAssertEqual(expectedUser.email, user.email)
XCTAssertEqual(expectedUser.createdDate, user.createdDate)
case .failure(let error):
XCTFail(error.localizedDescription)
}
})
}

func testGetUserData_WithInvalidToken_ReturnsUnauthorized() {
guard let _ = try? session.setData(resource: "invalid_token_401", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 401

client.getUserData(token: "", completion: { result in
switch result {
case .success:
XCTFail("Fetched current user without valid access token")
case .failure(let error):
XCTAssertEqual(error as? WFError, WFError.unauthorized)
}
})
}

func testGetUserCollections_WithValidToken_FetchesUserCollections() {
guard let _ = try? session.setData(resource: "test_fetch_user_collections", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 200

let expectedCollections = [
WFCollection(
alias: "matt",
title: "Matt",
description: "My great blog!",
styleSheet: "",
isPublic: true,
views: 46,
email: "[email protected]"
),
WFCollection(
alias: "new-blog",
title: "Test Blog",
description: "Another great blog!",
styleSheet: "",
isPublic: false,
views: 0,
email: "[email protected]"
)
]

client.getUserCollections(token: "00000000-0000-0000-0000-000000000000", completion: { result in
switch result {
case .success(let collections):
XCTAssertEqual(collections[0].alias, expectedCollections[0].alias)
XCTAssertEqual(collections[0].title, expectedCollections[0].title)
XCTAssertEqual(collections[0].description, expectedCollections[0].description)
XCTAssertEqual(collections[0].styleSheet, expectedCollections[0].styleSheet)
XCTAssertEqual(collections[0].isPublic, expectedCollections[0].isPublic)
XCTAssertEqual(collections[0].views, expectedCollections[0].views)
XCTAssertEqual(collections[0].email, expectedCollections[0].email)
XCTAssertEqual(collections[1].alias, expectedCollections[1].alias)
XCTAssertEqual(collections[1].title, expectedCollections[1].title)
XCTAssertEqual(collections[1].description, expectedCollections[1].description)
XCTAssertEqual(collections[1].styleSheet, expectedCollections[1].styleSheet)
XCTAssertEqual(collections[1].isPublic, expectedCollections[1].isPublic)
XCTAssertEqual(collections[1].views, expectedCollections[1].views)
XCTAssertEqual(collections[1].email, expectedCollections[1].email)
case .failure(let error):
XCTFail(error.localizedDescription)
}
})
}

func testGetUserCollections_WithInvalidToken_ReturnsUnauthorized() {
guard let _ = try? session.setData(resource: "invalid_token_401", fileExt: "json", for: self) else {
XCTFail("Error opening test resource file")
return
}

session.expectedStatusCode = 401

client.getUserCollections(token: "", completion: { result in
switch result {
case .success:
XCTFail("Fetched current user without valid access token")
case .failure(let error):
XCTAssertEqual(error as? WFError, WFError.unauthorized)
}
})
}
}
1 change: 1 addition & 0 deletions Tests/WriteFreelyTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import XCTest
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(WriteFreelyClientCollectionTests.allTests),
testCase(WriteFreelyClientUserTests.allTests),
]
}
#endif
Loading