diff --git a/tests/client_test.go b/tests/client_test.go new file mode 100644 index 0000000..18e3f04 --- /dev/null +++ b/tests/client_test.go @@ -0,0 +1,94 @@ +package main + +import ( + "net/http" + "os" + "strconv" + "testing" + + taiga "github.com/theriverman/taigo" +) + +var ( + comTestClient *taiga.Client = nil + dummyProjectID int +) + +func setupClient() { + if comTestClient != nil { + return // client already set; skipping + } + + url, ok := os.LookupEnv("env_ServerURL") + if !ok { + panic("Missing Environment Variable: env_ServerURL") + } + username, ok := os.LookupEnv("env_ServerUsername") + if !ok { + panic("Missing Environment Variable: env_ServerUsername") + } + password, ok := os.LookupEnv("env_ServerPassword") + if !ok { + panic("Missing Environment Variable: env_ServerPassword") + } + loginType := os.Getenv("env_LoginType") + if loginType == "" { + loginType = "normal" + } + dProjID, ok := os.LookupEnv("env_DummyProjectID") + if !ok { + panic("Missing Environment Variable: env_ServerPassword") + } + pid, err := strconv.Atoi(dProjID) + if err != nil { + panic("env_DummyProjectID: Invalid Project ID integer") + } else { + dummyProjectID = pid + } + + // Create client + client := taiga.Client{ + BaseURL: url, + HTTPClient: &http.Client{}, + } + // Initialise client (authenticates to Taiga) + err = client.Initialise() + if err != nil { + panic(err) + } + err = client.AuthByCredentials(&taiga.Credentials{ + Type: "normal", + Username: username, + Password: password, + }) + if err != nil { + panic(err) + } + comTestClient = &client +} + +func teardownClient() { + comTestClient = nil +} + +func TestClient(t *testing.T) { + setupClient() + + var makeurltests = []struct { + in []string + out string + }{ + {[]string{"epics"}, "https://api.taiga.io/api/v1/epics"}, + {[]string{"epics", "5"}, "https://api.taiga.io/api/v1/epics/5"}, + {[]string{"epics", "bulk_create"}, "https://api.taiga.io/api/v1/epics/bulk_create"}, + {[]string{"epics", "attachments", "5"}, "https://api.taiga.io/api/v1/epics/attachments/5"}, + } + + for _, tt := range makeurltests { + s := comTestClient.MakeURL(tt.in...) + if s != tt.out { + t.Errorf("got %q, want %q", s, tt.out) + } + } + +} diff --git a/tests/epics_test.go b/tests/epics_test.go new file mode 100644 index 0000000..8aeff6b --- /dev/null +++ b/tests/epics_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "testing" + + taiga "github.com/theriverman/taigo" +) + +func TestEpics(t *testing.T) { + setupClient() + + // List Epics + epics, err := comTestClient.Epic.List(&taiga.EpicsQueryParams{Project: dummyProjectID}) + if err != nil { + t.Error(err) + } + if len(epics) == 0 { + t.Error("Returned Epic list was empty.") + } + + // Create Epic + epic := &taiga.Epic{ + Subject: "Test Epic by Taigo", + Project: dummyProjectID, + } + epic, err = comTestClient.Epic.Create(epic) + if err != nil { + t.Error(err) + t.FailNow() + } + + // Edit Epic + epic.Description = "Added some text here via Client.Epic.Edit()" + epic, err = comTestClient.Epic.Edit(epic) + if err != nil { + t.Error(err) + } + + // Get Epic + e1, err := comTestClient.Epic.Get(epic.ID) + if (err != nil) || (e1.ID != epic.ID) { + t.Error(err) + } + + // Get Epic by Ref + e2, err := comTestClient.Epic.GetByRef(epic.Ref, &taiga.Project{ID: dummyProjectID}) + if (err != nil) || (e2.ID != epic.ID) { + t.Error(err) + } + + // Delete Epic by ID + _, err = comTestClient.Epic.Delete(epic.ID) + if err != nil { + t.Error(err) + } + + // Destroy taiga.Client{} + teardownClient() +}