Skip to content

Commit 57733b5

Browse files
authored
Merge pull request #38 from brokenhandsio/vapor3
Add missing commits
2 parents 8262116 + fff440e commit 57733b5

25 files changed

+360
-59
lines changed

Sources/SteamPress/Config/BlogGlobalPageInformation.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ public struct BlogGlobalPageInformation: Encodable {
88
public let websiteURL: URL
99
public let currentPageURL: URL
1010
}
11-
12-
#warning("Test this gets set correctly in route handler")

Sources/SteamPress/Extensions/URL+Converters.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ extension URL {
2525

2626
extension Request {
2727
func urlWithHTTPSIfReverseProxy() throws -> URL {
28+
guard var componets = URLComponents(url: self.http.url, resolvingAgainstBaseURL: false) else {
29+
throw SteamPressError(identifier: "SteamPressError", "Failed to get componets of url from \(self.http.url.absoluteString)")
30+
}
31+
2832
if self.http.headers["X-Forwarded-Proto"].first == "https" {
29-
guard var componets = URLComponents(url: self.http.url, resolvingAgainstBaseURL: false) else {
30-
throw SteamPressError(identifier: "SteamPressError", "Failed to get componets of url from \(self.http.url.absoluteString)")
31-
}
3233
componets.scheme = "https"
33-
guard let url = componets.url else {
34-
throw SteamPressError(identifier: "SteamPressError", "Failed to convert components to URL")
35-
}
36-
return url
3734
}
38-
return self.http.url
35+
36+
componets.query = nil
37+
38+
guard let url = componets.url else {
39+
throw SteamPressError(identifier: "SteamPressError", "Failed to convert components to URL")
40+
}
41+
return url
3942
}
4043
}
4144

Sources/SteamPress/Provider.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import Vapor
22
import Authentication
33

4-
public struct Provider<P: BlogPresenter, AP: BlogAdminPresenter>: Vapor.Provider {
5-
6-
public static var repositoryName: String {
7-
return "steampress"
8-
}
4+
public struct Provider: Vapor.Provider {
95

106
let blogPath: String?
117
let feedInformation: FeedInformation
128
let postsPerPage: Int
139
let enableAuthorPages: Bool
1410
let enableTagPages: Bool
15-
let blogPresenter: P
16-
let blogAdminPresenter: AP
1711
let pathCreator: BlogPathCreator
1812

1913
/**
@@ -28,35 +22,28 @@ public struct Provider<P: BlogPresenter, AP: BlogAdminPresenter>: Vapor.Provider
2822
or not. Defaults to true.
2923
- Parameter enableTagsPages: Flag used to determine whether to publicy expose the tags endpoints or not.
3024
Defaults to true.
31-
- Parameter blogPresenter: The presenter to generate templates with. Defaults to LeafBlogPresenter.
32-
- Parameter blogAdminPresenter: The presenter to generate templates for the admin section. Defaults to LeadBlogAdminPresenter.
3325
*/
3426
public init(
3527
blogPath: String? = nil,
3628
feedInformation: FeedInformation = FeedInformation(),
3729
postsPerPage: Int = 10,
3830
enableAuthorPages: Bool = true,
39-
enableTagPages: Bool = true,
40-
blogPresenter: P,
41-
blogAdminPresenter: AP) {
31+
enableTagPages: Bool = true) {
4232
self.blogPath = blogPath
4333
self.feedInformation = feedInformation
4434
self.postsPerPage = postsPerPage
45-
#warning("Default to sensible ones in the constructor")
4635
self.enableAuthorPages = enableAuthorPages
4736
self.enableTagPages = enableTagPages
48-
self.blogPresenter = blogPresenter
49-
self.blogAdminPresenter = blogAdminPresenter
5037
self.pathCreator = BlogPathCreator(blogPath: self.blogPath)
5138
}
5239

5340
public func register(_ services: inout Services) throws {
5441
services.register(BlogPresenter.self) { _ in
55-
return self.blogPresenter
42+
return ViewBlogPresenter()
5643
}
5744

5845
services.register(BlogAdminPresenter.self) { _ in
59-
return self.blogAdminPresenter
46+
return ViewBlogAdminPresenter(pathCreator: self.pathCreator)
6047
}
6148

6249
try services.register(AuthenticationProvider())

Tests/SteamPressTests/APITests/APITagControllerTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class APITagControllerTests: XCTestCase {
77
// MARK: - Tests
88

99
func testThatAllTagsAreReturnedFromAPI() throws {
10-
let testWorld = try TestWorld.create()
10+
var testWorld = try TestWorld.create()
1111

1212
let tag1 = try testWorld.context.repository.addTag(name: "Vapor3")
1313
let tag2 = try testWorld.context.repository.addTag(name: "Engineering")
@@ -16,7 +16,10 @@ class APITagControllerTests: XCTestCase {
1616

1717
XCTAssertEqual(tags[0].name, tag1.name)
1818
XCTAssertEqual(tags[1].name, tag2.name)
19+
20+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
1921
}
22+
2023
}
2124

2225
struct BlogTagJSON: Content {

Tests/SteamPressTests/AdminTests/AccessControlTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class AccessControlTests: XCTestCase {
1515
testWorld = try! TestWorld.create(path: "blog")
1616
user = testWorld.createUser()
1717
}
18+
19+
override func tearDown() {
20+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
21+
}
1822

1923
// MARK: - Tests
2024

Tests/SteamPressTests/AdminTests/AdminPageTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import XCTest
33
import SteamPress
44

55
class AdminPageTests: XCTestCase {
6+
67
func testAdminPagePassesCorrectInformationToPresenter() throws {
7-
let testWorld = try TestWorld.create()
8+
var testWorld = try TestWorld.create()
89
let user = testWorld.createUser(username: "leia")
910
let testData1 = try testWorld.createPost(author: user)
1011
let testData2 = try testWorld.createPost(title: "A second post", author: user)
@@ -22,5 +23,7 @@ class AdminPageTests: XCTestCase {
2223
XCTAssertEqual(presenter.adminViewPageInformation?.loggedInUser.username, user.username)
2324
XCTAssertEqual(presenter.adminViewPageInformation?.websiteURL.absoluteString, "")
2425
XCTAssertEqual(presenter.adminViewPageInformation?.currentPageURL.absoluteString, "/admin/")
26+
27+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
2528
}
2629
}

Tests/SteamPressTests/AdminTests/AdminPostTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class AdminPostTests: XCTestCase {
1919
testWorld = try! TestWorld.create()
2020
user = testWorld.createUser(username: "leia")
2121
}
22+
23+
override func tearDown() {
24+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
25+
}
2226

2327
// MARK: - Post Creation
2428

Tests/SteamPressTests/AdminTests/AdminUserTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class AdminUserTests: XCTestCase {
1919
testWorld = try! TestWorld.create()
2020
user = testWorld.createUser(name: "Leia", username: "leia")
2121
}
22+
23+
override func tearDown() {
24+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
25+
}
2226

2327
// MARK: - User Creation
2428

Tests/SteamPressTests/AdminTests/LoginTests.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class LoginTests: XCTestCase {
2525
testWorld = try! TestWorld.create(path: "blog")
2626
user = testWorld.createUser()
2727
}
28+
29+
override func tearDown() {
30+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
31+
}
2832

2933
// MARK: - Tests
3034

@@ -43,23 +47,23 @@ class LoginTests: XCTestCase {
4347
let sessionCookie = loginResponse.http.cookies["steampress-session"]
4448
var adminRequest = HTTPRequest(method: .GET, url: URL(string: "/blog/admin")!)
4549
adminRequest.cookies["steampress-session"] = sessionCookie
46-
let wrappedAdminRequest = Request(http: adminRequest, using: testWorld.context.app)
50+
let wrappedAdminRequest = Request(http: adminRequest, using: testWorld.context.app!)
4751

4852
let adminResponse = try testWorld.getResponse(to: wrappedAdminRequest)
4953

5054
XCTAssertEqual(adminResponse.http.status, .ok)
5155

5256
var logoutRequest = HTTPRequest(method: .POST, url: URL(string: "/blog/admin/logout")!)
5357
logoutRequest.cookies["steampress-session"] = sessionCookie
54-
let wrappedLogoutRequest = Request(http: logoutRequest, using: testWorld.context.app)
58+
let wrappedLogoutRequest = Request(http: logoutRequest, using: testWorld.context.app!)
5559
let logoutResponse = try testWorld.getResponse(to: wrappedLogoutRequest)
5660

5761
XCTAssertEqual(logoutResponse.http.status, .seeOther)
5862
XCTAssertEqual(logoutResponse.http.headers[.location].first, "/blog/")
5963

6064
var secondAdminRequest = HTTPRequest(method: .GET, url: URL(string: "/blog/admin")!)
6165
secondAdminRequest.cookies["steampress-session"] = sessionCookie
62-
let wrappedSecondRequest = Request(http: secondAdminRequest, using: testWorld.context.app)
66+
let wrappedSecondRequest = Request(http: secondAdminRequest, using: testWorld.context.app!)
6367
let loggedOutAdminResponse = try testWorld.getResponse(to: wrappedSecondRequest)
6468

6569
XCTAssertEqual(loggedOutAdminResponse.http.status, .seeOther)
@@ -273,9 +277,32 @@ class LoginTests: XCTestCase {
273277
let cookie = loginResponse.http.cookies["steampress-session"]
274278
var adminRequest = HTTPRequest(method: .GET, url: URL(string: "/blog/admin")!)
275279
adminRequest.cookies["steampress-session"] = cookie
276-
let wrappedAdminRequest = Request(http: adminRequest, using: testWorld.context.app)
280+
let wrappedAdminRequest = Request(http: adminRequest, using: testWorld.context.app!)
277281
let response = try testWorld.getResponse(to: wrappedAdminRequest)
278282

279283
XCTAssertEqual(loginResponse.http.cookies["steampress-session"]?.expires, response.http.cookies["steampress-session"]?.expires)
280284
}
285+
286+
func testCorrectPageInformationForLogin() throws {
287+
_ = try testWorld.getResponse(to: "/blog/admin/login")
288+
XCTAssertNil(blogPresenter.loginPageInformation?.disqusName)
289+
XCTAssertNil(blogPresenter.loginPageInformation?.googleAnalyticsIdentifier)
290+
XCTAssertNil(blogPresenter.loginPageInformation?.siteTwitterHandler)
291+
XCTAssertNil(blogPresenter.loginPageInformation?.loggedInUser)
292+
XCTAssertEqual(blogPresenter.loginPageInformation?.currentPageURL.absoluteString, "/blog/admin/login")
293+
XCTAssertEqual(blogPresenter.loginPageInformation?.websiteURL.absoluteString, "")
294+
}
295+
296+
func testSettingEnvVarsWithPageInformationForLoginPage() throws {
297+
let googleAnalytics = "ABDJIODJWOIJIWO"
298+
let twitterHandle = "3483209fheihgifffe"
299+
let disqusName = "34829u48932fgvfbrtewerg"
300+
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
301+
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
302+
setenv("BLOG_DISQUS_NAME", disqusName, 1)
303+
_ = try testWorld.getResponse(to: "/blog/admin/login")
304+
XCTAssertEqual(blogPresenter.loginPageInformation?.disqusName, disqusName)
305+
XCTAssertEqual(blogPresenter.loginPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
306+
XCTAssertEqual(blogPresenter.loginPageInformation?.siteTwitterHandler, twitterHandle)
307+
}
281308
}

Tests/SteamPressTests/BlogTests/AuthorTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class AuthorTests: XCTestCase {
2323
user = testWorld.createUser(username: "leia")
2424
postData = try! testWorld.createPost(author: user)
2525
}
26+
27+
override func tearDown() {
28+
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
29+
}
2630

2731
// MARK: - Tests
2832

@@ -67,6 +71,65 @@ class AuthorTests: XCTestCase {
6771
XCTAssertEqual(presenter.authorPosts?.first?.title, postData.post.title)
6872
XCTAssertEqual(presenter.authorPosts?.first?.contents, postData.post.contents)
6973
}
74+
75+
func testAuthorPageGetsCorrectPageInformation() throws {
76+
_ = try testWorld.getResponse(to: authorsRequestPath)
77+
XCTAssertNil(presenter.authorPageInformation?.disqusName)
78+
XCTAssertNil(presenter.authorPageInformation?.googleAnalyticsIdentifier)
79+
XCTAssertNil(presenter.authorPageInformation?.siteTwitterHandler)
80+
XCTAssertNil(presenter.authorPageInformation?.loggedInUser)
81+
XCTAssertEqual(presenter.authorPageInformation?.currentPageURL.absoluteString, authorsRequestPath)
82+
XCTAssertEqual(presenter.authorPageInformation?.websiteURL.absoluteString, "")
83+
}
84+
85+
func testAuthorPageInformationGetsLoggedInUser() throws {
86+
let user = testWorld.createUser()
87+
_ = try testWorld.getResponse(to: authorsRequestPath, loggedInUser: user)
88+
XCTAssertEqual(presenter.authorPageInformation?.loggedInUser?.username, user.username)
89+
}
90+
91+
func testSettingEnvVarsWithPageInformation() throws {
92+
let googleAnalytics = "ABDJIODJWOIJIWO"
93+
let twitterHandle = "3483209fheihgifffe"
94+
let disqusName = "34829u48932fgvfbrtewerg"
95+
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
96+
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
97+
setenv("BLOG_DISQUS_NAME", disqusName, 1)
98+
_ = try testWorld.getResponse(to: authorsRequestPath)
99+
XCTAssertEqual(presenter.authorPageInformation?.disqusName, disqusName)
100+
XCTAssertEqual(presenter.authorPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
101+
XCTAssertEqual(presenter.authorPageInformation?.siteTwitterHandler, twitterHandle)
102+
}
103+
104+
func testCorrectPageInformationForAllAuthors() throws {
105+
_ = try testWorld.getResponse(to: allAuthorsRequestPath)
106+
XCTAssertNil(presenter.allAuthorsPageInformation?.disqusName)
107+
XCTAssertNil(presenter.allAuthorsPageInformation?.googleAnalyticsIdentifier)
108+
XCTAssertNil(presenter.allAuthorsPageInformation?.siteTwitterHandler)
109+
XCTAssertNil(presenter.allAuthorsPageInformation?.loggedInUser)
110+
XCTAssertEqual(presenter.allAuthorsPageInformation?.currentPageURL.absoluteString, allAuthorsRequestPath)
111+
XCTAssertEqual(presenter.allAuthorsPageInformation?.websiteURL.absoluteString, "")
112+
}
113+
114+
func testPageInformationGetsLoggedInUserForAllAuthors() throws {
115+
let user = testWorld.createUser()
116+
_ = try testWorld.getResponse(to: allAuthorsRequestPath, loggedInUser: user)
117+
XCTAssertEqual(presenter.allAuthorsPageInformation?.loggedInUser?.username, user.username)
118+
}
119+
120+
func testSettingEnvVarsWithPageInformationForAllAuthors() throws {
121+
let googleAnalytics = "ABDJIODJWOIJIWO"
122+
let twitterHandle = "3483209fheihgifffe"
123+
let disqusName = "34829u48932fgvfbrtewerg"
124+
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
125+
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
126+
setenv("BLOG_DISQUS_NAME", disqusName, 1)
127+
_ = try testWorld.getResponse(to: allAuthorsRequestPath)
128+
XCTAssertEqual(presenter.allAuthorsPageInformation?.disqusName, disqusName)
129+
XCTAssertEqual(presenter.allAuthorsPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
130+
XCTAssertEqual(presenter.allAuthorsPageInformation?.siteTwitterHandler, twitterHandle)
131+
}
132+
70133

71134
// MARK: - Pagination Tests
72135
func testAuthorViewOnlyGetsTheSpecifiedNumberOfPosts() throws {

0 commit comments

Comments
 (0)