Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from shuheiktgw/add_key_pair_integration_tests
Browse files Browse the repository at this point in the history
Add integration tests for key pair endpoints
  • Loading branch information
shuheiktgw authored May 18, 2019
2 parents 00ed074 + ea8217b commit b3797c3
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 1 deletion.
2 changes: 1 addition & 1 deletion init_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
integrationRepoSlug = "shuheiktgwtest/go-travis-test"
integrationRepoId uint = 20783933
integrationTravisToken = os.Getenv("TRAVIS_API_AUTH_TOKEN")
integrationUrl = ApiOrgUrl
integrationUrl = ApiComUrl
integrationUserId uint = 1362503
)

Expand Down
224 changes: 224 additions & 0 deletions key_pair_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build integration

package travis

import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"net/http"
"testing"
)

var (
integrationPrivateGitHubRepoSlug = "shuheiktgwtest/go-travis-test-private"
integrationPrivateGitHubRepoId uint = 9221409
)

func TestKeyPairService_Integration_CRUD_ByRepoSlug(t *testing.T) {
// Make sure no key is registered
integrationClient.KeyPair.DeleteByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug)

v, err := generateKey()
if err != nil {
t.Fatalf("unexpected error occured while creating a key pair: %s", err)
}

// CreateByRepoSlug
b := KeyPairBody{Description: "test", Value: v}
k, res, err := integrationClient.KeyPair.CreateByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug, &b)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusCreated {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "test"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// FindByRepoSlug
k, res, err = integrationClient.KeyPair.FindByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "test"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// UpdateByRepoSlug
b = KeyPairBody{Description: "updated"}
k, res, err = integrationClient.KeyPair.UpdateByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug, &b)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "updated"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// DeleteByRepoSlug
res, err = integrationClient.KeyPair.DeleteByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusNoContent {
t.Fatalf("invalid http status: %s", res.Status)
}
}

func TestKeyPairService_Integration_CRUD_ByRepoId(t *testing.T) {
// Make sure no key is registered
integrationClient.KeyPair.DeleteByRepoId(context.TODO(), integrationPrivateGitHubRepoId)

v, err := generateKey()
if err != nil {
t.Fatalf("unexpected error occured while creating a key pair: %s", err)
}

// CreateByRepoSlug
b := KeyPairBody{Description: "test", Value: v}
k, res, err := integrationClient.KeyPair.CreateByRepoId(context.TODO(), integrationPrivateGitHubRepoId, &b)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusCreated {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "test"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// FindByRepoSlug
k, res, err = integrationClient.KeyPair.FindByRepoId(context.TODO(), integrationPrivateGitHubRepoId)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "test"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// UpdateByRepoSlug
b = KeyPairBody{Description: "updated"}
k, res, err = integrationClient.KeyPair.UpdateByRepoId(context.TODO(), integrationPrivateGitHubRepoId, &b)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if got, want := *k.Description, "updated"; got != want {
t.Fatalf("invalid description: gpt: %v, want: %v", got, want)
}

// DeleteByRepoSlug
res, err = integrationClient.KeyPair.DeleteByRepoId(context.TODO(), integrationPrivateGitHubRepoId)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusNoContent {
t.Fatalf("invalid http status: %s", res.Status)
}
}

func TestGeneratedKeyPairService_Integration_Create_Read_ByRepoSlug(t *testing.T) {
// CreateGeneratedByRepoSlug
_, res, err := integrationClient.GeneratedKeyPair.CreateByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusCreated {
t.Fatalf("invalid http status: %s", res.Status)
}

// FindGeneratedByRepoSlug
_, res, err = integrationClient.GeneratedKeyPair.FindByRepoSlug(context.TODO(), integrationPrivateGitHubRepoSlug)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}
}

func TestGeneratedKeyPairService_Integration_Create_Read_ByRepoId(t *testing.T) {
// CreateGeneratedByRepoId
_, res, err := integrationClient.GeneratedKeyPair.CreateByRepoId(context.TODO(), integrationPrivateGitHubRepoId)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusCreated {
t.Fatalf("invalid http status: %s", res.Status)
}

// FindGeneratedByRepoId
_, res, err = integrationClient.GeneratedKeyPair.FindByRepoId(context.TODO(), integrationPrivateGitHubRepoId)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}
}

func generateKey() (string, error) {
reader := rand.Reader
bitSize := 2048

key, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
return "", err
}

block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}

return string(pem.EncodeToMemory(block)), nil
}

0 comments on commit b3797c3

Please sign in to comment.