Skip to content

Commit

Permalink
Add Projects() test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 9, 2019
1 parent 3315050 commit 8c23d8e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mite/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mite_test

const testApiKey = "f00bar"
const testClientVersion = "vX"
const testUserAgent = "mite-go/" + testClientVersion + " (+github.com/leanovate/mite-go)"

type recorder struct {
method string
url string
contentType string
userAgent string
miteKey string
}
68 changes: 68 additions & 0 deletions mite/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package mite_test

import (
"fmt"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

const projectResponse = `{
"project": {
"id": 643,
"name": "Open-Source",
"note": "valvat, memento et all.",
"customer_id": 291,
"customer_name": "Yolk",
"budget": 0,
"budget_type": "minutes",
"hourly_rate": 6000,
"archived": false,
"active_hourly_rate": "hourly_rate",
"hourly_rates_per_service": [
{
"service_id": 31272,
"hourly_rate": 4500
},
{
"service_id": 149228,
"hourly_rate": 5500
}
],
"created_at": "2011-08-17T12:06:57+02:00",
"updated_at": "2015-02-19T10:53:10+01:00"
}
}`

func TestApi_Projects(t *testing.T) {
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("[%s]", projectResponse)))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
projects, err := api.Projects()

assert.Nil(t, err)
assert.Equal(t, []*domain.Project{{
Id: domain.NewProjectId(643),
Name: "Open-Source",
Note: "valvat, memento et all."}}, projects)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/projects.json", rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

0 comments on commit 8c23d8e

Please sign in to comment.