Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #155 from MITLibraries/128_roles
Browse files Browse the repository at this point in the history
Publisher and Static Pages Specs
  • Loading branch information
JPrevost committed Apr 7, 2015
2 parents d1747d7 + 52126d0 commit c992206
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 17 deletions.
34 changes: 20 additions & 14 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,30 @@ object Application extends Controller with Security {
Ok(views.html.publisher.index())
}

def newPublisher = Action { implicit request => //mustAuthenticate { username => implicit request =>
def newPublisher = isAuthenticated { identity =>
implicit request =>
Ok(views.html.publisher.create(null, pubForm))
}

def publisher(id: Int) = Action { implicit request => {
val userName = "richard"//session.get("username").getOrElse("")
val user = if ( play.api.Play.isTest(play.api.Play.current) ) {
User.findById(1).get.identity
} else {
request.session.get("connected").getOrElse("")
}

Publisher.findById(id).map( pub =>
Ok(views.html.publisher.show(pub, User.findByName(userName)))
Ok(views.html.publisher.show(pub, User.findByIdentity(user)))
).getOrElse(NotFound(views.html.static.trouble("No such publisher: " + id)))
}
}

def createPublisher = Action { implicit request => //isAuthenticated { username => implicit request =>
def createPublisher = isAuthenticated { identity =>
implicit request =>
pubForm.bindFromRequest.fold(
errors => BadRequest(views.html.publisher.create(null, errors)),
value => {
val user = User.findById(1).get
val pub = Publisher.make(user.id, value.tag, value.name, value.description, value.category, value.status, value.link, value.logo)
val pub = Publisher.make(identity.id, value.tag, value.name, value.description, value.category, value.status, value.link, value.logo)
Redirect(routes.Application.editPublisher(pub.id))
}
)
Expand All @@ -178,18 +184,18 @@ object Application extends Controller with Security {
}
}

private def ownsPublisher(username: String, pub: Publisher, result: Result)(implicit request: Request[AnyContent]): Result = {
val user = User.findByName(username).get
private def ownsPublisher(user: User, pub: Publisher, result: Result)(implicit request: Request[AnyContent]): Result = {
if (user.hasPublisher(pub.id)) {
result
} else {
Unauthorized(views.html.static.trouble("You are not authorized"))
}
}

def editPublisher(id: Int) = Action { implicit request => //isAuthenticated { username => implicit request =>
def editPublisher(id: Int) = isAuthenticated { identity =>
implicit request =>
Publisher.findById(id).map( pub =>
ownsPublisher("richard", pub, Ok(views.html.publisher.edit(pub)))
ownsPublisher(identity, pub, Ok(views.html.publisher.edit(pub)))
).getOrElse(NotFound(views.html.static.trouble("No such publisher: " + id)))
}

Expand Down Expand Up @@ -245,13 +251,13 @@ object Application extends Controller with Security {

def newHarvest(id: Int) = Action { implicit request =>
Publisher.findById(id).map( pub =>
ownsPublisher(/*username*/"richard", pub, Ok(views.html.harvest.create(pub, harvestForm)))
ownsPublisher(User.findById(1).get, pub, Ok(views.html.harvest.create(pub, harvestForm)))
).getOrElse(NotFound(views.html.static.trouble("No such publisher: " + id)))
}

def createHarvest(id: Int) = Action { implicit request =>
val pub = Publisher.findById(id).get
ownsPublisher(/*username*/"richard", pub,
ownsPublisher(User.findById(1).get, pub,
harvestForm.bindFromRequest.fold(
errors => BadRequest(views.html.harvest.create(pub, errors)),
value => {
Expand Down Expand Up @@ -299,13 +305,13 @@ object Application extends Controller with Security {

def newCollection(id: Int) = Action { implicit request => //isAuthenticated { username => implicit request =>
Publisher.findById(id).map( pub =>
ownsPublisher(/*username*/"richard", pub, Ok(views.html.collection.create(pub, collForm)))
ownsPublisher(User.findById(1).get, pub, Ok(views.html.collection.create(pub, collForm)))
).getOrElse(NotFound(views.html.static.trouble("No such publisher: " + id)))
}

def createCollection(id: Int) = Action { implicit request => //isAuthenticated { username => implicit request =>
val pub = Publisher.findById(id).get
ownsPublisher(/*username*/"richard", pub,
ownsPublisher(User.findById(1).get, pub,
collForm.bindFromRequest.fold(
errors => BadRequest(views.html.collection.create(pub, errors)),
value => {
Expand Down
2 changes: 1 addition & 1 deletion app/views/publisher/create.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@textarea(pubForm("description"))
@inputText(pubForm("link"))
@inputText(pubForm("logo"))
<input type="submit" value="Create">
<input id="submit" type="submit" value="Create">
}
</div>
<div class="col-md-8">
Expand Down
2 changes: 1 addition & 1 deletion app/views/publisher/show.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h2 itemprop="name">@pub.name</h2>
<p>Registered @HubUtils.fmtDate(pub.created)</P>
<p>@HubUtils.pluralize(pub.itemCount, "article") deposited</p>
<span>
@if(! pub.link.isEmpty) {
@if(pub.link.isDefined) {
<a rel="tooltip" title="Visit publisher site" href="@pub.link" class="btn btn-primary btn-large">Site &raquo;</a>
}
@if(!user.isEmpty && user.get.hasPublisher(pub.id)) {
Expand Down
2 changes: 1 addition & 1 deletion app/views/static/about.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ <h2>About TopicHub</h2>
<li>In application web pages, metadata is expressed using the <a href="http://schema.org">Schema.org</a> vocabulary.</li>
<li>Topics identified by the hub are syndicated as <a href="http://www.atomenabled.org">Atom</a> feeds containing items.</li>
</ul>
<p>TopicHub is built upon an open-source, Apache 2 licensed platform available on <a href="https://github.com/richardrodgers/topichub">GitHub</a>.</p>
<p>TopicHub is built upon an open-source, Apache 2 licensed platform available on <a href="https://github.com/MITLibraries/scoap3hub">GitHub</a>.</p>
}
148 changes: 148 additions & 0 deletions test/integration/PublisherPagesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import org.specs2.mutable._
import org.specs2.runner._

import play.api.test._
import play.api.test.Helpers._
import org.fest.assertions.Assertions.assertThat
import play.api.Application
import play.api.Play
import play.api.Play.current
import models.{ Publisher, User, Subscriber }

/**
* An integration test will fire up a whole play application in a real (or headless) browser
*/
class PublisherPagesSpec extends Specification {

def create_user(role: String) = User.make("bob", "bob@example.com", role, "identity")
def make_subscriber(userid: Int) = Subscriber.make(userid, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

"Publisher pages" should {
"index" should {
"display information about publishers" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/publishers")
assertThat(browser.title()).isEqualTo("Publishers - TopicHub")
browser.pageSource must contain("Contribute your content to TopicHub")
}

"display sign up link" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/publishers")
browser.pageSource must contain("/publishers/create")
}

"display links to browse exisiting publisher categories" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some(""), Some(""))

browser.goTo("http://localhost:" + port + "/publishers")
browser.pageSource must contain("/publishers/browse?filter=category&amp;value=pubcat")
}
}

"browse categories" should {
"display links to publishers" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some(""), Some(""))

browser.goTo("http://localhost:" + port + "/publishers")
browser.$("a[href*='pubcat']").click();
assertThat(browser.title()).isEqualTo("Publisher Browse - TopicHub")
}
}

"view publisher" should {
"display publishers page" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some("http://www.example.com"), Some(""))
browser.goTo("http://localhost:" + port + "/publisher/" + pub.id)
assertThat(browser.title()).isEqualTo("Publisher - TopicHub")
browser.pageSource must contain("No articles deposited")
browser.$("a[href*='http://www.example.com']").getText() must equalTo("Site »")
}

"with no publisher link displays no link" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", None, Some(""))
browser.goTo("http://localhost:" + port + "/publisher/" + pub.id)
browser.pageSource must not contain("Site »")
}

"display links to publisher collections" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
skipped
}

"edit link" should {
"displays to user associated with publisher" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val sub = make_subscriber(user.id)
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some("http://www.example.com"), Some(""))
browser.goTo("http://localhost:" + port + "/publisher/" + pub.id)
browser.pageSource must contain("/publisher/1/edit")
}

"not display to non logged in user" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
skipped(": need to be able to create a publisher without signing in user during test")
}

"not display to logged in user not associated with publisher" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub_user = User.make("pub", "pub@example.com", "some roles", "another_identity")
val pub = Publisher.make(pub_user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some("http://www.example.com"), Some(""))
browser.goTo("http://localhost:" + port + "/publisher/" + pub.id)
browser.pageSource must not contain("/publisher/1/edit")
}
}
}

"create publisher" should {
"redirect to login if not signed in" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/publishers")
browser.$("a[href*='/publishers/create']").click()
browser.pageSource must contain("Log in with your MIT ID")
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub")
}

"display create publisher form if signed in" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
browser.goTo("http://localhost:" + port + "/publishers")
browser.$("a[href*='/publishers/create']").click()
assertThat(browser.title()).isEqualTo("Create Publisher - TopicHub")
}

"creates publisher on submit" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
browser.goTo("http://localhost:" + port + "/publishers/create")
browser.$("#tag").text("New Publisher Tag")
browser.$("#name").text("New Publisher Name")
browser.$("#description").text("New Publisher Description")
browser.$("#submit").click
assertThat(browser.title()).isEqualTo("Edit Publisher - TopicHub")
}
}

"edit publisher" should {
"redirects to login if not signed in" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
skipped(": need to be able to create a publisher without signing in user during test")
}

"redirects to trouble if user does not own publisher" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub_user = User.make("pub", "pub@example.com", "some roles", "another_identity")
val pub = Publisher.make(pub_user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some("http://www.example.com"), Some(""))

browser.goTo("http://localhost:" + port + "/publisher/1/edit")
browser.pageSource must contain("Reason: You are not authorized")
}

"displays edit page if user owns publisher" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val user = create_user("norole")
val pub = Publisher.make(user.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some("http://www.example.com"), Some(""))

browser.goTo("http://localhost:" + port + "/publisher/1/edit")
assertThat(browser.title()).isEqualTo("Edit Publisher - TopicHub")
browser.pageSource must contain("/publisher/" + pub.id + "/create")
browser.pageSource must contain("/publisher/" + pub.id + "/createH")
}
}
}
}
65 changes: 65 additions & 0 deletions test/integration/StaticPagesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import org.specs2.mutable._
import org.specs2.runner._

import play.api.test._
import play.api.test.Helpers._
import org.fest.assertions.Assertions.assertThat
import play.api.Application
import play.api.Play
import play.api.Play.current

/**
* An integration test will fire up a whole play application in a real (or headless) browser
*/
class StaticPagesSpec extends Specification {

"home" should {
"displays welcome message" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port)
browser.pageSource must contain("Welcome to SCOAP")
assertThat(browser.title()).isEqualTo("SCOAP3 - TopicHub")
}

"displays Schemes with counts" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
// load pubmodel and cmodel
// counts should all be zero
// add some items with topic schmes, counts should new values
skipped
}
}

"about" should {
"display about message" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/about")
browser.pageSource must contain("About TopicHub")
assertThat(browser.title()).isEqualTo("About - TopicHub")
}

"link to github" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/about")
browser.pageSource must contain("https://github.com/MITLibraries/scoap3hub")
}
}

"feedback" should {
"displays form" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/feedback")
browser.pageSource must contain("Feedback")
assertThat(browser.title()).isEqualTo("Feedback - TopicHub")
}

"submitting without required fields retains filled out fields" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/feedback")
browser.$("#email").text("popcorn@example.com")
browser.$("#reply").click
browser.$("#sendbutton").click
assertThat(browser.title()).isEqualTo("Feedback - TopicHub")
browser.pageSource must contain("popcorn@example.com")
browser.pageSource must contain("This field is required")
}

"submitted form generates an email" in new WithBrowser(app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
skipped
}
}
}

0 comments on commit c992206

Please sign in to comment.