Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 0df0bdb

Browse files
committed
Added some tests and fixed a few bugs
1 parent a5e553e commit 0df0bdb

File tree

3 files changed

+110
-31
lines changed

3 files changed

+110
-31
lines changed

Sources/AdminPanel/Models/BackendUsers/BackendUserForm.swift

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct BackendUserForm {
1515
let password: String
1616
var passwordErrors: [String]
1717

18-
let repeatPassword: String?
18+
let repeatPassword: String
1919
var repeatPasswordErrors: [String]
2020

2121
let sendMail: Bool
@@ -42,7 +42,7 @@ public struct BackendUserForm {
4242
self.email = email ?? ""
4343
self.role = role ?? ""
4444
self.password = password ?? ""
45-
self.repeatPassword = repeatPassword
45+
self.repeatPassword = repeatPassword ?? ""
4646
self.sendMail = sendMail ?? false
4747
self.randomPassword = randomPassword ?? false
4848
self.shouldResetPassword = shouldResetPassword ?? false
@@ -61,7 +61,6 @@ public struct BackendUserForm {
6161
let sendEmail = json["sendEmail"]?.bool
6262
let password = json["password"]?.string
6363
let repeatPassword = json["repeatPassword"]?.string
64-
let randomPassword = json["randomPassword"]?.bool
6564

6665
return validate(
6766
name: name,
@@ -70,8 +69,7 @@ public struct BackendUserForm {
7069
shouldResetPassword: shouldResetPassword,
7170
sendEmail: sendEmail,
7271
password: password,
73-
repeatPassword: repeatPassword,
74-
randomPassword: randomPassword
72+
repeatPassword: repeatPassword
7573
)
7674
}
7775

@@ -83,7 +81,6 @@ public struct BackendUserForm {
8381
let sendEmail = content["sendEmail"]?.bool
8482
let password = content["password"]?.string
8583
let repeatPassword = content["repeatPassword"]?.string
86-
let randomPassword = content["randomPassword"]?.bool
8784

8885
return validate(
8986
name: name,
@@ -92,8 +89,7 @@ public struct BackendUserForm {
9289
shouldResetPassword: shouldResetPassword,
9390
sendEmail: sendEmail,
9491
password: password,
95-
repeatPassword: repeatPassword,
96-
randomPassword: randomPassword
92+
repeatPassword: repeatPassword
9793
)
9894
}
9995

@@ -104,9 +100,10 @@ public struct BackendUserForm {
104100
shouldResetPassword: Bool?,
105101
sendEmail: Bool?,
106102
password: String?,
107-
repeatPassword: String?,
108-
randomPassword: Bool?
103+
repeatPassword: String?
109104
) -> (BackendUserForm, hasErrors: Bool) {
105+
var shouldResetPassword = shouldResetPassword
106+
var password = password
110107
var hasErrors = false
111108

112109
var nameErrors: [String] = []
@@ -131,48 +128,54 @@ public struct BackendUserForm {
131128
hasErrors = true
132129
}
133130

134-
let nameCharactercount = name!.utf8.count
131+
let nameCharactercount = name?.utf8.count ?? 0
135132
if nameCharactercount < 1 || nameCharactercount > 191 {
136133
nameErrors.append("Must be between 1 and 191 characters long")
137134
hasErrors = true
138135
}
139136

140-
let emailCharactercount = email!.utf8.count
137+
let emailCharactercount = email?.utf8.count ?? 0
141138
if emailCharactercount < 1 || emailCharactercount > 191 {
142139
emailErrors.append("Must be between 1 and 191 characters long")
143140
hasErrors = true
144141
}
145142

146-
if role!.utf8.count > 191 {
143+
if (role?.utf8.count ?? 0) > 191 {
147144
nameErrors.append("Must be less than 191 characters long")
148145
hasErrors = true
149146
}
150147

151-
let randomPassword = randomPassword ?? (password == nil)
152-
let password = password ?? String.randomAlphaNumericString(10)
153-
154-
let passwordCharactercount = password.utf8.count
155-
if passwordCharactercount < 1 || passwordCharactercount > 191 {
156-
passwordErrors.append("Must be between 1 and 191 characters long")
157-
hasErrors = true
158-
}
159-
148+
let randomPassword = (password == nil && repeatPassword == nil)
160149
if !randomPassword {
161150
if password != repeatPassword {
162151
repeatPasswordErrors.append("Passwords do not match")
163152
hasErrors = true
164153
}
165-
166-
if repeatPassword == nil {
167-
repeatPasswordErrors.append(requiredFieldError)
168-
hasErrors = true
154+
155+
if let password = password {
156+
let passwordCharactercount = password.utf8.count
157+
if passwordCharactercount < 1 || passwordCharactercount > 191 {
158+
passwordErrors.append("Must be between 1 and 191 characters long")
159+
hasErrors = true
160+
}
169161
} else {
170-
let passwordRepeatCharacterCount = repeatPassword!.utf8.count
162+
passwordErrors.append(requiredFieldError)
163+
hasErrors = true
164+
}
165+
166+
if let repeatPassword = repeatPassword {
167+
let passwordRepeatCharacterCount = repeatPassword.utf8.count
171168
if passwordRepeatCharacterCount < 1 || passwordRepeatCharacterCount > 191 {
172169
repeatPasswordErrors.append("Must be between 1 and 191 characters long")
173170
hasErrors = true
174171
}
172+
} else {
173+
repeatPasswordErrors.append(requiredFieldError)
174+
hasErrors = true
175175
}
176+
} else {
177+
password = String.randomAlphaNumericString(10)
178+
shouldResetPassword = true
176179
}
177180

178181
let user = BackendUserForm(

Tests/AdminPanelTests/AdminPanelTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import XCTest
2-
import AdminPanel
3-
@testable import AdminPanelTests
2+
@testable import AdminPanel
43

54
class AdminPanelTests: XCTestCase {
65
func testRandomString() {
76
let length = 10
87
let randomString = String.randomAlphaNumericString(length)
98
XCTAssertEqual(randomString.characters.count, length)
109
}
11-
12-
10+
1311
static var allTests : [(String, (AdminPanelTests) -> () throws -> Void)] {
1412
return [
1513
("testRandomString", testRandomString),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import XCTest
2+
3+
@testable import AdminPanel
4+
5+
class BackendUserTests: XCTestCase {
6+
func testSendEmail() {
7+
let (form, _) = BackendUserForm.validating(["sendEmail": "true"])
8+
XCTAssertTrue(form.sendMail)
9+
}
10+
11+
func testPasswordsMatch() {
12+
let (form, hasErrors) = BackendUserForm.validate(
13+
name: nil,
14+
email: nil,
15+
role: nil,
16+
shouldResetPassword: nil,
17+
sendEmail: false,
18+
password: "test",
19+
repeatPassword: "test"
20+
)
21+
XCTAssert(hasErrors)
22+
XCTAssertEqual(form.passwordErrors.count, 0)
23+
}
24+
25+
func testErrorOnMissingPassword() {
26+
let (form, _) = BackendUserForm.validate(
27+
name: nil,
28+
email: nil,
29+
role: nil,
30+
shouldResetPassword: nil,
31+
sendEmail: nil,
32+
password: nil,
33+
repeatPassword: "test"
34+
)
35+
36+
XCTAssertFalse(form.randomPassword)
37+
XCTAssertEqual(form.passwordErrors.count, 1)
38+
XCTAssertEqual(form.repeatPasswordErrors.count, 1)
39+
XCTAssert(form.password.isEmpty)
40+
XCTAssertEqual(form.repeatPassword, "test")
41+
}
42+
43+
func testErrorOnMissingPasswordRepeat() {
44+
let (form, _) = BackendUserForm.validate(
45+
name: nil,
46+
email: nil,
47+
role: nil,
48+
shouldResetPassword: nil,
49+
sendEmail: nil,
50+
password: "test",
51+
repeatPassword: nil
52+
)
53+
54+
XCTAssertFalse(form.randomPassword)
55+
XCTAssertEqual(form.passwordErrors.count, 0)
56+
XCTAssertEqual(form.repeatPasswordErrors.count, 2)
57+
XCTAssert(form.repeatPassword.isEmpty)
58+
XCTAssertEqual(form.password, "test")
59+
}
60+
61+
func testPasswordsDoNotMatch() {
62+
let (form, _) = BackendUserForm.validate(
63+
name: nil,
64+
email: nil,
65+
role: nil,
66+
shouldResetPassword: nil,
67+
sendEmail: false,
68+
password: nil,
69+
repeatPassword: nil
70+
)
71+
72+
XCTAssert(form.randomPassword)
73+
XCTAssert(form.shouldResetPassword)
74+
XCTAssertEqual(form.passwordErrors.count, 0)
75+
XCTAssertEqual(form.repeatPasswordErrors.count, 0)
76+
XCTAssertFalse(form.password.isEmpty)
77+
}
78+
}

0 commit comments

Comments
 (0)