Skip to content

Commit

Permalink
Avoid primitive obsession
Browse files Browse the repository at this point in the history
Also remove some duplication between login tests
  • Loading branch information
ShezHsky committed Jul 18, 2017
1 parent 76f7606 commit 9a9ac47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Eurofurence.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
A034379A14212C5E996E6321 /* Pods_Eurofurence.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A81694C40AA0176899E15028 /* Pods_Eurofurence.framework */; };
CA2B4FA41F1E9FA600F007E1 /* WhenLoggingIn.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA2B4FA31F1E9FA600F007E1 /* WhenLoggingIn.swift */; };
CA2B4FA61F1EA1B300F007E1 /* ApplicationTestBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA2B4FA51F1EA1B300F007E1 /* ApplicationTestBuilder.swift */; };
CA2B4FA81F1EA72A00F007E1 /* LoginArguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA2B4FA71F1EA72A00F007E1 /* LoginArguments.swift */; };
CAD780371F1D415A002A8333 /* MessagesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD780361F1D415A002A8333 /* MessagesViewController.swift */; };
CAD780391F1D41C4002A8333 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD780381F1D41C4002A8333 /* LoginViewController.swift */; };
CAD780401F1D4C2A002A8333 /* WhenLoginStateChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD7803F1F1D4C2A002A8333 /* WhenLoginStateChanges.swift */; };
Expand Down Expand Up @@ -430,6 +431,7 @@
B4939A463603D9F022E19825 /* Pods-EurofurenceTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EurofurenceTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-EurofurenceTests/Pods-EurofurenceTests.debug.xcconfig"; sourceTree = "<group>"; };
CA2B4FA31F1E9FA600F007E1 /* WhenLoggingIn.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WhenLoggingIn.swift; path = "Application Tests/WhenLoggingIn.swift"; sourceTree = "<group>"; };
CA2B4FA51F1EA1B300F007E1 /* ApplicationTestBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ApplicationTestBuilder.swift; path = "Application Tests/ApplicationTestBuilder.swift"; sourceTree = "<group>"; };
CA2B4FA71F1EA72A00F007E1 /* LoginArguments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LoginArguments.swift; path = Login/LoginArguments.swift; sourceTree = "<group>"; };
CAD780361F1D415A002A8333 /* MessagesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesViewController.swift; sourceTree = "<group>"; };
CAD780381F1D41C4002A8333 /* LoginViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
CAD7803F1F1D4C2A002A8333 /* WhenLoginStateChanges.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WhenLoginStateChanges.swift; path = "Application Tests/WhenLoginStateChanges.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1126,6 +1128,7 @@
CAD780421F1D4CA3002A8333 /* LoginController.swift */,
CAD7804F1F1E2D13002A8333 /* LoginCredentialStore.swift */,
CAD780591F1E8E7D002A8333 /* KeychainLoginCredentialStore.swift */,
CA2B4FA71F1EA72A00F007E1 /* LoginArguments.swift */,
);
name = Login;
sourceTree = "<group>";
Expand Down Expand Up @@ -1680,6 +1683,7 @@
D6F3D275FF81A313B9213277 /* AnnouncementCell.swift in Sources */,
D6F3DAD590C051C8D0060172 /* EventViewController.swift in Sources */,
D6F3D62A784F07D9C7A84DC8 /* KnowledgeEntryViewController.swift in Sources */,
CA2B4FA81F1EA72A00F007E1 /* LoginArguments.swift in Sources */,
78F555991F10D5A800BDA7D4 /* ReplaceViewControllersSegue.swift in Sources */,
9A0F87E51F1BAD2C00FF294B /* LocalNotificationStore.swift in Sources */,
D6F3D2440B74E17B9A008DE0 /* NewsViewController.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Eurofurence/Application/EurofurenceApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class EurofurenceApplication: LoginStateObserver {
}
}

func login(registrationNumber: Int, username: String, password: String) {
func login(_ arguments: LoginArguments) {
do {
let postArguments: [String : Any] = ["RegNo": registrationNumber,
"Username": username,
"Password": password]
let postArguments: [String : Any] = ["RegNo": arguments.registrationNumber,
"Username": arguments.username,
"Password": arguments.password]
let jsonData = try JSONSerialization.data(withJSONObject: postArguments, options: [])
jsonPoster.post("https://app.eurofurence.org/api/v2/Tokens/RegSys", body: jsonData)
} catch {
Expand Down
17 changes: 17 additions & 0 deletions Eurofurence/Services/Login/LoginArguments.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// LoginArguments.swift
// Eurofurence
//
// Created by Thomas Sherwood on 18/07/2017.
// Copyright © 2017 Eurofurence. All rights reserved.
//

import Foundation

struct LoginArguments {

var registrationNumber: Int
var username: String
var password: String

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class ApplicationTestBuilder {
application.registerRemoteNotifications(deviceToken: deviceToken)
}

func login(registrationNumber: Int = 0, username: String = "", password: String = "") {
let arguments = LoginArguments(registrationNumber: registrationNumber, username: username, password: password)
application.login(arguments)
}

func notifyUserLoggedIn(_ token: String = "", expires: Date = .distantFuture) {
capturingLoginController.notifyUserLoggedIn(token, expires: expires)
}
Expand Down
8 changes: 4 additions & 4 deletions EurofurenceTests/Application Tests/WhenLoggingIn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WhenLoggingIn: XCTestCase {

func testTheLoginEndpointShouldReceievePOSTRequest() {
let context = ApplicationTestBuilder().build()
context.application.login(registrationNumber: 0, username: "", password: "")
context.login()

XCTAssertEqual("https://app.eurofurence.org/api/v2/Tokens/RegSys", context.jsonPoster.postedURL)
}
Expand All @@ -26,23 +26,23 @@ class WhenLoggingIn: XCTestCase {
func testTheLoginRequestShouldReceieveJSONPayloadWithRegNo() {
let context = ApplicationTestBuilder().build()
let registrationNumber = 42
context.application.login(registrationNumber: registrationNumber, username: "", password: "")
context.login(registrationNumber: registrationNumber)

XCTAssertEqual(registrationNumber, context.jsonPoster.postedJSONValue(forKey: "RegNo"))
}

func testTheLoginRequestShouldReceieveJSONPayloadWithUsername() {
let context = ApplicationTestBuilder().build()
let username = "Some awesome guy"
context.application.login(registrationNumber: 0, username: username, password: "")
context.login(username: username)

XCTAssertEqual(username, context.jsonPoster.postedJSONValue(forKey: "Username"))
}

func testTheLoginRequestShouldReceieveJSONPayloadWithPassword() {
let context = ApplicationTestBuilder().build()
let password = "It's a secrent"
context.application.login(registrationNumber: 0, username: "", password: password)
context.login(password: password)

XCTAssertEqual(password, context.jsonPoster.postedJSONValue(forKey: "Password"))
}
Expand Down

0 comments on commit 9a9ac47

Please sign in to comment.