github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/event_test.go (about)

     1  package ccv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  
    10  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Event", func() {
    17  	var client *Client
    18  
    19  	BeforeEach(func() {
    20  		client, _ = NewTestClient()
    21  	})
    22  
    23  	Describe("GetEvents", func() {
    24  		var (
    25  			events     []Event
    26  			warnings   Warnings
    27  			executeErr error
    28  		)
    29  
    30  		JustBeforeEach(func() {
    31  			events, warnings, executeErr = client.GetEvents(
    32  				Query{Key: TargetGUIDFilter, Values: []string{"some-target-guid"}},
    33  				Query{Key: OrderBy, Values: []string{"-created_at"}},
    34  				Query{Key: PerPage, Values: []string{"1"}})
    35  		})
    36  
    37  		var response string
    38  		BeforeEach(func() {
    39  			response = fmt.Sprintf(`{
    40    "pagination": {
    41      "total_results": 3,
    42      "total_pages": 2,
    43      "next": {
    44        "href": "%s/v3/audit_events?page=2&per_page=2"
    45      },
    46      "previous": null
    47    },
    48    "resources": [
    49      {
    50        "guid": "some-event-guid",
    51        "created_at": "2016-06-08T16:41:23Z",
    52        "updated_at": "2016-06-08T16:41:26Z",
    53        "type": "audit.app.update",
    54        "actor": {
    55          "guid": "d144abe3-3d7b-40d4-b63f-2584798d3ee5",
    56          "type": "user",
    57          "name": "admin"
    58        },
    59        "target": {
    60          "guid": "2e3151ba-9a63-4345-9c5b-6d8c238f4e55",
    61          "type": "app",
    62          "name": "my-app"
    63        },
    64        "data": {
    65          "request": {
    66            "recursive": true
    67          }
    68        },
    69        "space": {
    70          "guid": "cb97dd25-d4f7-4185-9e6f-ad6e585c207c"
    71        },
    72        "organization": {
    73          "guid": "d9be96f5-ea8f-4549-923f-bec882e32e3c"
    74        },
    75        "links": {
    76          "self": {
    77            "href": "https://api.example.org/v3/audit_events/a595fe2f-01ff-4965-a50c-290258ab8582"
    78          }
    79        }
    80      }
    81    ]
    82  }`, server.URL())
    83  		})
    84  
    85  		Context("when the event exists", func() {
    86  			BeforeEach(func() {
    87  				server.AppendHandlers(
    88  					CombineHandlers(
    89  						VerifyRequest(http.MethodGet, "/v3/audit_events", "target_guids=some-target-guid&order_by=-created_at&per_page=1"),
    90  						RespondWith(http.StatusAccepted, response, http.Header{"X-Cf-Warnings": {"warning"}}),
    91  					),
    92  				)
    93  			})
    94  
    95  			It("returns the event guid of the most recent event", func() {
    96  				timestamp, err := time.Parse(time.RFC3339, "2016-06-08T16:41:23Z")
    97  				Expect(err).ToNot(HaveOccurred())
    98  				Expect(executeErr).ToNot(HaveOccurred())
    99  				Expect(warnings).To(ConsistOf("warning"))
   100  				Expect(events).To(ConsistOf(
   101  					Event{
   102  						GUID:      "some-event-guid",
   103  						CreatedAt: timestamp,
   104  						Type:      "audit.app.update",
   105  						ActorName: "admin",
   106  						Data: map[string]interface{}{
   107  							"request": map[string]interface{}{
   108  								"recursive": true,
   109  							},
   110  						},
   111  					},
   112  				))
   113  			})
   114  		})
   115  
   116  		Context("when the request fails", func() {
   117  			BeforeEach(func() {
   118  				response := `{
   119    "errors": [
   120      {
   121        "code": 10008,
   122        "detail": "The request is semantically invalid: command presence",
   123        "title": "CF-UnprocessableEntity"
   124      },
   125      {
   126        "code": 10010,
   127        "detail": "App not found",
   128        "title": "CF-ResourceNotFound"
   129      }
   130    ]
   131  }`
   132  
   133  				server.AppendHandlers(
   134  					CombineHandlers(
   135  						VerifyRequest(http.MethodGet, "/v3/audit_events", "target_guids=some-target-guid&order_by=-created_at&per_page=1"),
   136  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning"}}),
   137  					),
   138  				)
   139  			})
   140  
   141  			It("returns CC warnings and error", func() {
   142  				Expect(executeErr).To(MatchError(ccerror.MultiError{
   143  					ResponseCode: http.StatusTeapot,
   144  					Errors: []ccerror.V3Error{
   145  						{
   146  							Code:   10008,
   147  							Detail: "The request is semantically invalid: command presence",
   148  							Title:  "CF-UnprocessableEntity",
   149  						},
   150  						{
   151  							Code:   10010,
   152  							Detail: "App not found",
   153  							Title:  "CF-ResourceNotFound",
   154  						},
   155  					},
   156  				}))
   157  				Expect(warnings).To(ConsistOf("warning"))
   158  			})
   159  		})
   160  	})
   161  })