1 | package test |
---|
2 | |
---|
3 | import org.specs2.mutable._ |
---|
4 | |
---|
5 | import play.api.test._ |
---|
6 | import play.api.test.Helpers._ |
---|
7 | |
---|
8 | class ApplicationSpec extends Specification { |
---|
9 | |
---|
10 | import models._ |
---|
11 | |
---|
12 | // -- Date helpers |
---|
13 | |
---|
14 | def dateIs(date: java.util.Date, str: String) = new java.text.SimpleDateFormat("yyyy-MM-dd").format(date) == str |
---|
15 | |
---|
16 | // -- |
---|
17 | |
---|
18 | "Application" should { |
---|
19 | |
---|
20 | "redirect to the computer list on /" in { |
---|
21 | |
---|
22 | val result = controllers.Application.index(FakeRequest()) |
---|
23 | |
---|
24 | status(result) must equalTo(SEE_OTHER) |
---|
25 | redirectLocation(result) must beSome.which(_ == "/computers") |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | "list computers on the the first page" in { |
---|
30 | running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { |
---|
31 | |
---|
32 | val result = controllers.Application.list(0, 2, "")(FakeRequest()) |
---|
33 | |
---|
34 | status(result) must equalTo(OK) |
---|
35 | contentAsString(result) must contain("574 computers found") |
---|
36 | |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | "filter computer by name" in { |
---|
41 | running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { |
---|
42 | |
---|
43 | val result = controllers.Application.list(0, 2, "Apple")(FakeRequest()) |
---|
44 | |
---|
45 | status(result) must equalTo(OK) |
---|
46 | contentAsString(result) must contain("13 computers found") |
---|
47 | |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | "create new computer" in { |
---|
52 | running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { |
---|
53 | |
---|
54 | val badResult = controllers.Application.save(FakeRequest()) |
---|
55 | |
---|
56 | status(badResult) must equalTo(BAD_REQUEST) |
---|
57 | |
---|
58 | val badDateFormat = controllers.Application.save( |
---|
59 | FakeRequest().withFormUrlEncodedBody("name" -> "FooBar", "introduced" -> "badbadbad", "company" -> "1") |
---|
60 | ) |
---|
61 | |
---|
62 | status(badDateFormat) must equalTo(BAD_REQUEST) |
---|
63 | contentAsString(badDateFormat) must contain("""<option value="1" selected>Apple Inc.</option>""") |
---|
64 | contentAsString(badDateFormat) must contain("""<input type="text" id="introduced" name="introduced" value="badbadbad" >""") |
---|
65 | contentAsString(badDateFormat) must contain("""<input type="text" id="name" name="name" value="FooBar" >""") |
---|
66 | |
---|
67 | val result = controllers.Application.save( |
---|
68 | FakeRequest().withFormUrlEncodedBody("name" -> "FooBar", "introduced" -> "2011-12-24", "company" -> "1") |
---|
69 | ) |
---|
70 | |
---|
71 | status(result) must equalTo(SEE_OTHER) |
---|
72 | redirectLocation(result) must beSome.which(_ == "/computers") |
---|
73 | flash(result).get("success") must beSome.which(_ == "Computer FooBar has been created") |
---|
74 | |
---|
75 | val list = controllers.Application.list(0, 2, "FooBar")(FakeRequest()) |
---|
76 | |
---|
77 | status(list) must equalTo(OK) |
---|
78 | contentAsString(list) must contain("One computer found") |
---|
79 | |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|