1// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package travis
7
8import (
9	"context"
10	"fmt"
11	"net/http"
12	"reflect"
13	"testing"
14)
15
16const testOwner = "shuheiktgw"
17const testGitHubId = 83472489
18
19func TestActiveService_FindByOwner(t *testing.T) {
20	client, mux, _, teardown := setup()
21	defer teardown()
22
23	mux.HandleFunc(fmt.Sprintf("/owner/%s/active", testOwner), func(w http.ResponseWriter, r *http.Request) {
24		testMethod(t, r, http.MethodGet)
25		testFormValues(t, r, values{"include": "active.builds"})
26		fmt.Fprint(w, `{"builds": [{"id":1,"number":"1","state":"created","duration":10}]}`)
27	})
28
29	opt := ActiveOption{Include: []string{"active.builds"}}
30	builds, _, err := client.Active.FindByOwner(context.Background(), testOwner, &opt)
31
32	if err != nil {
33		t.Errorf("Active.FindByOwner returned error: %v", err)
34	}
35
36	want := []*Build{{Id: testBuildId, Number: "1", State: BuildStateCreated, Duration: 10}}
37	if !reflect.DeepEqual(builds, want) {
38		t.Errorf("Active.FindByOwner returned %+v, want %+v", builds, want)
39	}
40}
41
42func TestActiveService_FindByGitHubId(t *testing.T) {
43	client, mux, _, teardown := setup()
44	defer teardown()
45
46	mux.HandleFunc(fmt.Sprintf("/owner/github_id/%d/active", testGitHubId), func(w http.ResponseWriter, r *http.Request) {
47		testMethod(t, r, http.MethodGet)
48		testFormValues(t, r, values{"include": "active.builds"})
49		fmt.Fprint(w, `{"builds": [{"id":1,"number":"1","state":"created","duration":10}]}`)
50	})
51
52	opt := ActiveOption{Include: []string{"active.builds"}}
53	builds, _, err := client.Active.FindByGitHubId(context.Background(), testGitHubId, &opt)
54
55	if err != nil {
56		t.Errorf("Active.FindByGitHubId returned error: %v", err)
57	}
58
59	want := []*Build{{Id: testBuildId, Number: "1", State: BuildStateCreated, Duration: 10}}
60	if !reflect.DeepEqual(builds, want) {
61		t.Errorf("Active.FindByGitHubId returned %+v, want %+v", builds, want)
62	}
63}
64