Skip to content

Commit

Permalink
Merge pull request #38 from brokenhandsio/vapor3
Browse files Browse the repository at this point in the history
Add missing commits
  • Loading branch information
0xTim authored Dec 20, 2019
2 parents 8262116 + fff440e commit 57733b5
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 59 deletions.
2 changes: 0 additions & 2 deletions Sources/SteamPress/Config/BlogGlobalPageInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ public struct BlogGlobalPageInformation: Encodable {
public let websiteURL: URL
public let currentPageURL: URL
}

#warning("Test this gets set correctly in route handler")
19 changes: 11 additions & 8 deletions Sources/SteamPress/Extensions/URL+Converters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ extension URL {

extension Request {
func urlWithHTTPSIfReverseProxy() throws -> URL {
guard var componets = URLComponents(url: self.http.url, resolvingAgainstBaseURL: false) else {
throw SteamPressError(identifier: "SteamPressError", "Failed to get componets of url from \(self.http.url.absoluteString)")
}

if self.http.headers["X-Forwarded-Proto"].first == "https" {
guard var componets = URLComponents(url: self.http.url, resolvingAgainstBaseURL: false) else {
throw SteamPressError(identifier: "SteamPressError", "Failed to get componets of url from \(self.http.url.absoluteString)")
}
componets.scheme = "https"
guard let url = componets.url else {
throw SteamPressError(identifier: "SteamPressError", "Failed to convert components to URL")
}
return url
}
return self.http.url

componets.query = nil

guard let url = componets.url else {
throw SteamPressError(identifier: "SteamPressError", "Failed to convert components to URL")
}
return url
}
}

Expand Down
21 changes: 4 additions & 17 deletions Sources/SteamPress/Provider.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import Vapor
import Authentication

public struct Provider<P: BlogPresenter, AP: BlogAdminPresenter>: Vapor.Provider {

public static var repositoryName: String {
return "steampress"
}
public struct Provider: Vapor.Provider {

let blogPath: String?
let feedInformation: FeedInformation
let postsPerPage: Int
let enableAuthorPages: Bool
let enableTagPages: Bool
let blogPresenter: P
let blogAdminPresenter: AP
let pathCreator: BlogPathCreator

/**
Expand All @@ -28,35 +22,28 @@ public struct Provider<P: BlogPresenter, AP: BlogAdminPresenter>: Vapor.Provider
or not. Defaults to true.
- Parameter enableTagsPages: Flag used to determine whether to publicy expose the tags endpoints or not.
Defaults to true.
- Parameter blogPresenter: The presenter to generate templates with. Defaults to LeafBlogPresenter.
- Parameter blogAdminPresenter: The presenter to generate templates for the admin section. Defaults to LeadBlogAdminPresenter.
*/
public init(
blogPath: String? = nil,
feedInformation: FeedInformation = FeedInformation(),
postsPerPage: Int = 10,
enableAuthorPages: Bool = true,
enableTagPages: Bool = true,
blogPresenter: P,
blogAdminPresenter: AP) {
enableTagPages: Bool = true) {
self.blogPath = blogPath
self.feedInformation = feedInformation
self.postsPerPage = postsPerPage
#warning("Default to sensible ones in the constructor")
self.enableAuthorPages = enableAuthorPages
self.enableTagPages = enableTagPages
self.blogPresenter = blogPresenter
self.blogAdminPresenter = blogAdminPresenter
self.pathCreator = BlogPathCreator(blogPath: self.blogPath)
}

public func register(_ services: inout Services) throws {
services.register(BlogPresenter.self) { _ in
return self.blogPresenter
return ViewBlogPresenter()
}

services.register(BlogAdminPresenter.self) { _ in
return self.blogAdminPresenter
return ViewBlogAdminPresenter(pathCreator: self.pathCreator)
}

try services.register(AuthenticationProvider())
Expand Down
5 changes: 4 additions & 1 deletion Tests/SteamPressTests/APITests/APITagControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class APITagControllerTests: XCTestCase {
// MARK: - Tests

func testThatAllTagsAreReturnedFromAPI() throws {
let testWorld = try TestWorld.create()
var testWorld = try TestWorld.create()

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

XCTAssertEqual(tags[0].name, tag1.name)
XCTAssertEqual(tags[1].name, tag2.name)

XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

}

struct BlogTagJSON: Content {
Expand Down
4 changes: 4 additions & 0 deletions Tests/SteamPressTests/AdminTests/AccessControlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class AccessControlTests: XCTestCase {
testWorld = try! TestWorld.create(path: "blog")
user = testWorld.createUser()
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Tests

Expand Down
5 changes: 4 additions & 1 deletion Tests/SteamPressTests/AdminTests/AdminPageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import XCTest
import SteamPress

class AdminPageTests: XCTestCase {

func testAdminPagePassesCorrectInformationToPresenter() throws {
let testWorld = try TestWorld.create()
var testWorld = try TestWorld.create()
let user = testWorld.createUser(username: "leia")
let testData1 = try testWorld.createPost(author: user)
let testData2 = try testWorld.createPost(title: "A second post", author: user)
Expand All @@ -22,5 +23,7 @@ class AdminPageTests: XCTestCase {
XCTAssertEqual(presenter.adminViewPageInformation?.loggedInUser.username, user.username)
XCTAssertEqual(presenter.adminViewPageInformation?.websiteURL.absoluteString, "")
XCTAssertEqual(presenter.adminViewPageInformation?.currentPageURL.absoluteString, "/admin/")

XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}
}
4 changes: 4 additions & 0 deletions Tests/SteamPressTests/AdminTests/AdminPostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class AdminPostTests: XCTestCase {
testWorld = try! TestWorld.create()
user = testWorld.createUser(username: "leia")
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Post Creation

Expand Down
4 changes: 4 additions & 0 deletions Tests/SteamPressTests/AdminTests/AdminUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class AdminUserTests: XCTestCase {
testWorld = try! TestWorld.create()
user = testWorld.createUser(name: "Leia", username: "leia")
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - User Creation

Expand Down
35 changes: 31 additions & 4 deletions Tests/SteamPressTests/AdminTests/LoginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class LoginTests: XCTestCase {
testWorld = try! TestWorld.create(path: "blog")
user = testWorld.createUser()
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Tests

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

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

XCTAssertEqual(adminResponse.http.status, .ok)

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

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

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

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

XCTAssertEqual(loginResponse.http.cookies["steampress-session"]?.expires, response.http.cookies["steampress-session"]?.expires)
}

func testCorrectPageInformationForLogin() throws {
_ = try testWorld.getResponse(to: "/blog/admin/login")
XCTAssertNil(blogPresenter.loginPageInformation?.disqusName)
XCTAssertNil(blogPresenter.loginPageInformation?.googleAnalyticsIdentifier)
XCTAssertNil(blogPresenter.loginPageInformation?.siteTwitterHandler)
XCTAssertNil(blogPresenter.loginPageInformation?.loggedInUser)
XCTAssertEqual(blogPresenter.loginPageInformation?.currentPageURL.absoluteString, "/blog/admin/login")
XCTAssertEqual(blogPresenter.loginPageInformation?.websiteURL.absoluteString, "")
}

func testSettingEnvVarsWithPageInformationForLoginPage() throws {
let googleAnalytics = "ABDJIODJWOIJIWO"
let twitterHandle = "3483209fheihgifffe"
let disqusName = "34829u48932fgvfbrtewerg"
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
setenv("BLOG_DISQUS_NAME", disqusName, 1)
_ = try testWorld.getResponse(to: "/blog/admin/login")
XCTAssertEqual(blogPresenter.loginPageInformation?.disqusName, disqusName)
XCTAssertEqual(blogPresenter.loginPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
XCTAssertEqual(blogPresenter.loginPageInformation?.siteTwitterHandler, twitterHandle)
}
}
63 changes: 63 additions & 0 deletions Tests/SteamPressTests/BlogTests/AuthorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class AuthorTests: XCTestCase {
user = testWorld.createUser(username: "leia")
postData = try! testWorld.createPost(author: user)
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Tests

Expand Down Expand Up @@ -67,6 +71,65 @@ class AuthorTests: XCTestCase {
XCTAssertEqual(presenter.authorPosts?.first?.title, postData.post.title)
XCTAssertEqual(presenter.authorPosts?.first?.contents, postData.post.contents)
}

func testAuthorPageGetsCorrectPageInformation() throws {
_ = try testWorld.getResponse(to: authorsRequestPath)
XCTAssertNil(presenter.authorPageInformation?.disqusName)
XCTAssertNil(presenter.authorPageInformation?.googleAnalyticsIdentifier)
XCTAssertNil(presenter.authorPageInformation?.siteTwitterHandler)
XCTAssertNil(presenter.authorPageInformation?.loggedInUser)
XCTAssertEqual(presenter.authorPageInformation?.currentPageURL.absoluteString, authorsRequestPath)
XCTAssertEqual(presenter.authorPageInformation?.websiteURL.absoluteString, "")
}

func testAuthorPageInformationGetsLoggedInUser() throws {
let user = testWorld.createUser()
_ = try testWorld.getResponse(to: authorsRequestPath, loggedInUser: user)
XCTAssertEqual(presenter.authorPageInformation?.loggedInUser?.username, user.username)
}

func testSettingEnvVarsWithPageInformation() throws {
let googleAnalytics = "ABDJIODJWOIJIWO"
let twitterHandle = "3483209fheihgifffe"
let disqusName = "34829u48932fgvfbrtewerg"
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
setenv("BLOG_DISQUS_NAME", disqusName, 1)
_ = try testWorld.getResponse(to: authorsRequestPath)
XCTAssertEqual(presenter.authorPageInformation?.disqusName, disqusName)
XCTAssertEqual(presenter.authorPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
XCTAssertEqual(presenter.authorPageInformation?.siteTwitterHandler, twitterHandle)
}

func testCorrectPageInformationForAllAuthors() throws {
_ = try testWorld.getResponse(to: allAuthorsRequestPath)
XCTAssertNil(presenter.allAuthorsPageInformation?.disqusName)
XCTAssertNil(presenter.allAuthorsPageInformation?.googleAnalyticsIdentifier)
XCTAssertNil(presenter.allAuthorsPageInformation?.siteTwitterHandler)
XCTAssertNil(presenter.allAuthorsPageInformation?.loggedInUser)
XCTAssertEqual(presenter.allAuthorsPageInformation?.currentPageURL.absoluteString, allAuthorsRequestPath)
XCTAssertEqual(presenter.allAuthorsPageInformation?.websiteURL.absoluteString, "")
}

func testPageInformationGetsLoggedInUserForAllAuthors() throws {
let user = testWorld.createUser()
_ = try testWorld.getResponse(to: allAuthorsRequestPath, loggedInUser: user)
XCTAssertEqual(presenter.allAuthorsPageInformation?.loggedInUser?.username, user.username)
}

func testSettingEnvVarsWithPageInformationForAllAuthors() throws {
let googleAnalytics = "ABDJIODJWOIJIWO"
let twitterHandle = "3483209fheihgifffe"
let disqusName = "34829u48932fgvfbrtewerg"
setenv("BLOG_GOOGLE_ANALYTICS_IDENTIFIER", googleAnalytics, 1)
setenv("BLOG_SITE_TWITTER_HANDLER", twitterHandle, 1)
setenv("BLOG_DISQUS_NAME", disqusName, 1)
_ = try testWorld.getResponse(to: allAuthorsRequestPath)
XCTAssertEqual(presenter.allAuthorsPageInformation?.disqusName, disqusName)
XCTAssertEqual(presenter.allAuthorsPageInformation?.googleAnalyticsIdentifier, googleAnalytics)
XCTAssertEqual(presenter.allAuthorsPageInformation?.siteTwitterHandler, twitterHandle)
}


// MARK: - Pagination Tests
func testAuthorViewOnlyGetsTheSpecifiedNumberOfPosts() throws {
Expand Down
14 changes: 14 additions & 0 deletions Tests/SteamPressTests/BlogTests/DisabledBlogTagTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import XCTest
import Vapor

class DisabledBlogTagTests: XCTestCase {
func testDisabledBlogTagsPath() throws {
var testWorld = try TestWorld.create(enableTagPages: false)
_ = try testWorld.createTag("Engineering")
let tagResponse = try testWorld.getResponse(to: "/tags/Engineering")
let allTagsResponse = try testWorld.getResponse(to: "/tags")

XCTAssertEqual(.notFound, tagResponse.http.status)
XCTAssertEqual(.notFound, allTagsResponse.http.status)
}
}
4 changes: 4 additions & 0 deletions Tests/SteamPressTests/BlogTests/IndexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class IndexTests: XCTestCase {
testWorld = try! TestWorld.create(postsPerPage: postsPerPage)
firstData = try! testWorld.createPost(title: "Test Path", slugUrl: "test-path")
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Tests

Expand Down
8 changes: 6 additions & 2 deletions Tests/SteamPressTests/BlogTests/PostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class PostTests: XCTestCase {
testWorld = try! TestWorld.create()
firstData = try! testWorld.createPost(title: "Test Path", slugUrl: "test-path")
}

override func tearDown() {
XCTAssertNoThrow(try testWorld.tryAsHardAsWeCanToShutdownApplication())
}

// MARK: - Tests

Expand All @@ -32,7 +36,7 @@ class PostTests: XCTestCase {
XCTAssertEqual(presenter.postAuthor?.username, firstData.author.username)
}

func testIndexGetsCorrectPageInformation() throws {
func testPostPageGetsCorrectPageInformation() throws {
_ = try testWorld.getResponse(to: blogPostPath)
XCTAssertNil(presenter.postPageInformation?.disqusName)
XCTAssertNil(presenter.postPageInformation?.googleAnalyticsIdentifier)
Expand All @@ -42,7 +46,7 @@ class PostTests: XCTestCase {
XCTAssertEqual(presenter.postPageInformation?.websiteURL.absoluteString, "")
}

func testIndexPageInformationGetsLoggedInUser() throws {
func testPostPageInformationGetsLoggedInUser() throws {
let user = testWorld.createUser()
_ = try testWorld.getResponse(to: blogPostPath, loggedInUser: user)
XCTAssertEqual(presenter.postPageInformation?.loggedInUser?.username, user.username)
Expand Down
Loading

0 comments on commit 57733b5

Please sign in to comment.