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 #159 from MITLibraries/128_roles
Browse files Browse the repository at this point in the history
Content Type Page Specs
  • Loading branch information
JPrevost committed Apr 9, 2015
2 parents 8343514 + 2975b80 commit a651820
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 32 deletions.
34 changes: 15 additions & 19 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -398,36 +398,31 @@ object Application extends Controller with Security {
)
)

def contentTypes = Action { implicit request => //isAuthenticated { username => implicit request =>
//isAnalyst(username, Ok(views.html.ctype_list(ContentType.all)))
def contentTypes = isAnalyst { identity => implicit request =>
Ok(views.html.content_type.index(ContentType.all))
}

def contentType(id: Int) = Action { implicit request => // isAuthenticated { username => implicit request =>
def contentType(id: Int) = isAnalyst { identity => implicit request =>
ContentType.findById(id).map( ctype =>
//isAnalyst(username, Ok(views.html.ctype(ctype, ctSchemeForm)))
Ok(views.html.content_type.show(ctype, ctSchemeForm))
).getOrElse(NotFound(views.html.static.trouble("No such content type")))
}

def newContentType = Action { implicit request => //isAuthenticated { username => implicit request =>
//isAnalyst(username, Ok(views.html.new_ctype(ctypeForm)))
Ok(views.html.content_type.create(ctypeForm))
def newContentType = isAnalyst { identity => implicit request =>
Ok(views.html.content_type.create(ctypeForm))
}

def createContentType = Action { implicit request => //isAuthenticated { username => implicit request =>
//isAnalyst(username,
ctypeForm.bindFromRequest.fold(
errors => BadRequest(views.html.content_type.create(errors)),
value => {
ContentType.create(value.tag, value.label, value.description, value.logo)
Redirect(routes.Application.contentTypes)
}
)
//)
def createContentType = isAnalyst { identity => implicit request =>
ctypeForm.bindFromRequest.fold(
errors => BadRequest(views.html.content_type.create(errors)),
value => {
ContentType.create(value.tag, value.label, value.description, value.logo)
Redirect(routes.Application.contentTypes)
}
)
}

def addContentTypeScheme(id: Int, relation: String) = Action { implicit request =>
def addContentTypeScheme(id: Int, relation: String) = isAnalyst { identity => implicit request =>
ctSchemeForm.bindFromRequest.fold(
errors => {
val ct = ContentType.findById(id).get
Expand All @@ -443,7 +438,8 @@ object Application extends Controller with Security {
)
}

def removeContentTypeScheme(cid: Int, sid: Int, relation: String) = Action { implicit request =>
def removeContentTypeScheme(cid: Int, sid: Int, relation: String) = isAnalyst {
identity => implicit request =>
ContentType.findById(cid).map( ctype =>
Scheme.findById(sid).map( scheme => {
ctype.removeScheme(scheme, relation)
Expand Down
6 changes: 3 additions & 3 deletions app/views/content_type/create.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
************************************************************************@
@(ctypeForm: Form[ContentType])@*(implicit hubContext: HubContext)*@
@import helper._
@layout.main("Content Types - TopicHub") {
<h2>Add a new content type</h2>
@layout.main("New Content Type - TopicHub") {
<h2>Add a new content type</h2>
@form(routes.Application.createContentType) {
@inputText(ctypeForm("tag"))
@inputText(ctypeForm("label"))
@inputText(ctypeForm("description"))
@inputText(ctypeForm("logo"))
<input type="submit" value="Create">
<input id="submit" type="submit" value="Create">
}
}
26 changes: 16 additions & 10 deletions app/views/content_type/show.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@
*****************************************************************************@
@(ctype: ContentType, scForm: Form[(Int, Int)])@*(implicit hubContext: HubContext)*@
@import helper._
@layout.main("Content Types - TopicHub") {
@layout.main("Content Type - TopicHub") {
<h2>Content Type: @ctype.tag</h2>
<h3>Metadata Schema</h3>
<ul>
@ctype.schemes("meta").map { scheme =>
<li><a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "meta")">remove</a></li>
<li>
<a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "meta")">remove</a>
</li>
}
</ul>
<h4>Add a new metadata scheme</h4>
@form(routes.Application.addContentTypeScheme(ctype.id, "meta")) {
@select(scForm("scheme_id"), options(Scheme.mapView))
<input type="submit" value="Create">
<input id="metadata_scheme_submit" type="submit" value="Create">
}
<h3>Index Schema</h3>
<ul>
@ctype.schemes("index").map { scheme =>
<li><a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "index")">remove</a></li>
<li>
<a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "index")">remove</a>
</li>
}
</ul>
<h4>Add a new index scheme</h4>
@form(routes.Application.addContentTypeScheme(ctype.id, "index")) {
@select(scForm("scheme_id"), options(Scheme.mapView))
<input type="submit" value="Create">
<input id="index_scheme_submit" type="submit" value="Create">
}
<h3>Topic Schema</h3>
<ul>
@ctype.schemes("topic").map { scheme =>
<li><a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "topic")">remove</a></li>
<li>
<a href="@routes.Application.scheme(scheme.id)">@scheme.tag</a>: @scheme.description
<a href="@routes.Application.removeContentTypeScheme(ctype.id, scheme.id, "topic")">remove</a>
</li>
}
</ul>
<h4>Add a new topic scheme</h4>
@form(routes.Application.addContentTypeScheme(ctype.id, "topic")) {
@select(scForm("scheme_id"), options(Scheme.mapView))
<input type="submit" value="Create">
<input id="topic_scheme_submit" type="submit" value="Create">
}
}
235 changes: 235 additions & 0 deletions test/integration/ContentTypePagesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
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.{ ContentType, Scheme, User }

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

def create_user(role: String) = User.make("bob", "bob@example.com", role, "current_user")

"Content Type pages" should {
"as an unauthenticated User" should {

// GET /ctypes
"accessing index redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/ctypes")
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub")
}

// GET /ctype/:id
"accessing content type show redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id)
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub")
}

// GET /ctypes/create
"accessing content type create form redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
browser.goTo("http://localhost:" + port + "/ctypes/create")
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub")
}

// POST /ctypes
"posting to content type create redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val action = route(FakeRequest(POST, "/ctypes")).get
redirectLocation(action) must beSome.which(_ == "/login")
ContentType.all.size must equalTo(0)
}

// POST /ctype/:id/scheme
"posting to add content type scheme redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
val s = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
val action = route(FakeRequest(POST, "/ctype/" + ct.id + "/scheme?relation=relation")).get
redirectLocation(action) must beSome.which(_ == "/login")
}

// GET /ctype/:id/scheme
"accessing remove Content Type Scheme redirects to login" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
val s = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
ct.addScheme(s, "relation")
ct.schemes("relation") must haveSize(1)
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id +
"/scheme?relation=relation&sid=" + s.id)
assertThat(browser.title()).isEqualTo("Login to SCOAP3 - TopicHub")
ct.schemes("relation") must haveSize(1)
}
}

"as a User lacking the Analyst role" should {

// GET /ctypes
"accessing index redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
browser.goTo("http://localhost:" + port + "/ctypes")
assertThat(browser.title()).isEqualTo("Error - TopicHub")
browser.pageSource must contain("You are not authorized")
}

// GET /ctype/:id
"accessing content type show redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id)
assertThat(browser.title()).isEqualTo("Error - TopicHub")
browser.pageSource must contain("You are not authorized")
}

// GET /ctypes/create
"accessing content type create form redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
browser.goTo("http://localhost:" + port + "/ctypes/create")
assertThat(browser.title()).isEqualTo("Error - TopicHub")
browser.pageSource must contain("You are not authorized")
}

// POST /ctypes
"posting to content type create redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
val action = route(FakeRequest(POST, "/ctypes")).get
redirectLocation(action) must beNone
contentAsString(action) must contain ("Reason: You are not authorized")
ContentType.all.size must equalTo(0)
}

// POST /ctype/:id/scheme
"posting to add content type scheme redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
val s = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
val action = route(FakeRequest(POST, "/ctype/" + ct.id + "/scheme?relation=relation")).get
redirectLocation(action) must beNone
contentAsString(action) must contain ("Reason: You are not authorized")
}

// GET /ctype/:id/scheme
"accessing remove Content Type Scheme redirects to trouble" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("somerole")
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
val s = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
ct.addScheme(s, "relation")
ct.schemes("relation") must haveSize(1)
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id +
"/scheme?relation=relation&sid=" + s.id)
assertThat(browser.title()).isEqualTo("Error - TopicHub")
browser.pageSource must contain("You are not authorized")
ct.schemes("relation") must haveSize(1)
}
}

"as a User with the Analyst role" should {

// GET /ctypes
"accessing index displays index" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
browser.goTo("http://localhost:" + port + "/ctypes")
assertThat(browser.title()).isEqualTo("Content Types - TopicHub")
}

// GET /ctype/:id
"accessing content type show displays content type page" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id)
assertThat(browser.title()).isEqualTo("Content Type - TopicHub")
browser.pageSource must contain(s"Content Type: ${ct.tag}")
}

// GET /ctypes/create
"accessing content type create form displays form" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
browser.goTo("http://localhost:" + port + "/ctypes/create")
assertThat(browser.title()).isEqualTo("New Content Type - TopicHub")
browser.pageSource must contain("""<form action="/ctypes" method="POST">""")
}

// POST /ctypes
"posting to content type create creates a content type" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
browser.goTo("http://localhost:" + port + "/ctypes/create")

// submit without filling out required elements
browser.$("#submit").click
browser.pageSource must contain("This field is required")
ContentType.all.size must equalTo(0)

// submit with required elements
browser.$("#tag").text("ct tag")
browser.$("#label").text("label thing")
browser.$("#description").text("super good description")
browser.$("#logo").text("logo")
browser.$("#submit").click
assertThat(browser.title()).isEqualTo("Content Types - TopicHub")
browser.pageSource must contain("super good description")
ContentType.all.size must equalTo(1)
}

// POST /ctype/:id/scheme
"posting to add content type scheme creates link" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
val ct = ContentType.make("c_tag", "label", "desc", Some("logo"))
val s = Scheme.make("abstract_tag", "gentype", "cat", "s_desc", Some("link"), Some("logo"))
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id)
ct.schemes("meta") must haveSize(0)
ct.schemes("topic") must haveSize(0)
ct.schemes("index") must haveSize(0)

browser.$("#metadata_scheme_submit").click
ct.schemes("meta") must haveSize(1)
ct.schemes("topic") must haveSize(0)
ct.schemes("index") must haveSize(0)

browser.$("#topic_scheme_submit").click
ct.schemes("meta") must haveSize(1)
ct.schemes("topic") must haveSize(1)
ct.schemes("index") must haveSize(0)

browser.$("#index_scheme_submit").click
ct.schemes("meta") must haveSize(1)
ct.schemes("topic") must haveSize(1)
ct.schemes("index") must haveSize(1)
}

// GET /ctype/:id/scheme
"accessing remove Content Type Scheme removes link" in new WithBrowser(
app = FakeApplication(additionalConfiguration = inMemoryDatabase())) {
create_user("analyst")
val ct = ContentType.make("tag", "label", "desc", Some("logo"))
val s = Scheme.make("tag", "gentype", "cat", "desc", Some("link"), Some("logo"))
ct.addScheme(s, "relation")
ct.schemes("relation") must haveSize(1)
browser.goTo("http://localhost:" + port + "/ctype/" + ct.id +
"/scheme?relation=relation&sid=" + s.id)
assertThat(browser.title()).isEqualTo("Content Type - TopicHub")
ct.schemes("relation") must haveSize(0)
}
}
}
}

0 comments on commit a651820

Please sign in to comment.