Skip to content

Commit

Permalink
Improve integration tests and no more use wiremock for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Jul 12, 2018
1 parent b84bf94 commit 43579c8
Show file tree
Hide file tree
Showing 97 changed files with 5,697 additions and 511 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ test_lib: ##@test Run lib tests

_test_lib:
@echo "${YELLOW}Running lib tests${RESET}"
@${MAKE} ensure_wiremock_is_up --no-print-directory
@go test -v ${TESTS_OPTS} ./gitlab/.
@echo "${GREEN}✔ Lib tests successfully passed${RESET}\n"

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ Visit the docs at http://godoc.org/github.com/plouc/go-gitlab-client/gitlab
- [x] Single user
- [x] Current user

#### SSH Keys

[gitlab api doc](http://api.gitlab.org/users.html#list-ssh-keys)

- [x] List SSH keys
- [x] List SSH keys for user
- [x] Single SSH key
- [x] Add SSH key
- [x] Add SSH key for user
- [x] Delete SSH key for current user
- [x] Delete SSH key for given user

#### Groups

[gitlab api doc](https://docs.gitlab.com/ce/api/groups.html)
Expand Down
10 changes: 8 additions & 2 deletions cli/cmd/get_project_badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"strconv"
)

func init() {
Expand All @@ -19,10 +20,15 @@ var getProjectBadgeCmd = &cobra.Command{
return err
}

color.Yellow("Fetching project's badge (project id: %s, badge id: %s)…", ids["project_id"], ids["badge_id"])
badgeId, err := strconv.Atoi(ids["badge_id"])
if err != nil {
return err
}

color.Yellow("Fetching project's badge (project id: %s, badge id: %s)…", ids["project_id"], badgeId)

loader.Start()
badge, meta, err := client.ProjectBadge(ids["project_id"], ids["badge_id"])
badge, meta, err := client.ProjectBadge(ids["project_id"], badgeId)
loader.Stop()
if err != nil {
return err
Expand Down
48 changes: 48 additions & 0 deletions cli/cmd/ls_ssh_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/plouc/go-gitlab-client/gitlab"
"github.com/spf13/cobra"
)

func init() {
lsCmd.AddCommand(lsSshKeysCmd)
}

func fetchSshKeys() {
color.Yellow("Fetching current user ssh keys…")

o := &gitlab.PaginationOptions{}
o.Page = page
o.PerPage = perPage

loader.Start()
keys, meta, err := client.CurrentUserSshKeys(o)
loader.Stop()
if err != nil {
fmt.Println(err.Error())
return
}

if len(keys) == 0 {
color.Red("No ssh key found")
} else {
sshKeysOutput(keys)
}

metaOutput(meta, true)

handlePaginatedResult(meta, fetchSshKeys)
}

var lsSshKeysCmd = &cobra.Command{
Use: "ssh-keys",
Aliases: []string{"sk"},
Short: "List current user ssh keys",
Run: func(cmd *cobra.Command, args []string) {
fetchSshKeys()
},
}
63 changes: 63 additions & 0 deletions cli/cmd/ls_user_ssh_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/plouc/go-gitlab-client/gitlab"
"github.com/spf13/cobra"
"strconv"
)

func init() {
lsCmd.AddCommand(lsUserSshKeysCmd)
}

func fetchUserSshKeys(userId int) {
color.Yellow("Fetching user %d ssh keys…", userId)

o := &gitlab.PaginationOptions{}
o.Page = page
o.PerPage = perPage

loader.Start()
keys, meta, err := client.CurrentUserSshKeys(o)
loader.Stop()
if err != nil {
fmt.Println(err.Error())
return
}

if len(keys) == 0 {
color.Red("No ssh key found for user: %d", userId)
} else {
sshKeysOutput(keys)
}

metaOutput(meta, true)

handlePaginatedResult(meta, func() {
fetchUserSshKeys(userId)
})
}

var lsUserSshKeysCmd = &cobra.Command{
Use: resourceCmd("user-ssh-keys", "user"),
Aliases: []string{"usk"},
Short: "List specific user ssh keys",
RunE: func(cmd *cobra.Command, args []string) error {
ids, err := config.aliasIdsOrArgs(currentAlias, "user", args)
if err != nil {
return err
}

userId, err := strconv.Atoi(ids["user_id"])
if err != nil {
return err
}

fetchUserSshKeys(userId)

return nil
},
}
36 changes: 36 additions & 0 deletions cli/cmd/output_ssh_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"

"github.com/olekukonko/tablewriter"
"github.com/plouc/go-gitlab-client/gitlab"
)

func sshKeysOutput(keys []*gitlab.SshKey) {
if outputFormat == "json" {
jsonOutput(keys)
} else if outputFormat == "yaml" {
yamlOutput(keys)
} else {
fmt.Fprintln(output, "")
table := tablewriter.NewWriter(output)
table.SetHeader([]string{
"Id",
"Title",
"Key",
"Created at",
})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
for _, key := range keys {
table.Append([]string{
fmt.Sprintf("%d", key.Id),
key.Title,
key.Key[:16] + "…",
key.CreatedAtRaw,
})
}
table.Render()
fmt.Fprintln(output, "")
}
}
15 changes: 9 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
version: '2'

services:

wiremock:
image: ${WIREMOCK_IMAGE}
entrypoint: java -Xms512M -Xmx1024M -jar wiremock.jar
volumes:
- ${PWD}/mocks/__files:/wiremock/__files
- ${PWD}/mocks/mappings:/wiremock/mappings
- ${PWD}/mocks/__files:/wiremock/__files:cached
- ${PWD}/mocks/mappings:/wiremock/mappings:cached
ports:
- 8080:8080

Expand All @@ -14,7 +16,8 @@ services:
command: /bin/sh -c "while [ 1 ]; do sleep 3600; done"
working_dir: /go/src/${GO_PKG_SRC_PATH}
volumes:
- ${PWD}/gitlab:/go/src/${GO_PKG_SRC_PATH}/gitlab
- ${PWD}/cli:/go/src/${GO_PKG_SRC_PATH}/cli
- ${PWD}/integration:/go/src/${GO_PKG_SRC_PATH}/integration
- ${PWD}/Makefile:/go/src/${GO_PKG_SRC_PATH}/Makefile
- ${PWD}/gitlab:/go/src/${GO_PKG_SRC_PATH}/gitlab:consistent
- ${PWD}/cli:/go/src/${GO_PKG_SRC_PATH}/cli:consistent
- ${PWD}/integration:/go/src/${GO_PKG_SRC_PATH}/integration:consistent
- ${PWD}/Makefile:/go/src/${GO_PKG_SRC_PATH}/Makefile:consistent
- ${PWD}/mocks:/go/src/${GO_PKG_SRC_PATH}/mocks:cached
5 changes: 3 additions & 2 deletions gitlab/badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitlab

import (
"encoding/json"
"strconv"
)

const (
Expand Down Expand Up @@ -31,10 +32,10 @@ func (g *Gitlab) ProjectBadges(projectId string, o *PaginationOptions) ([]*Badge
return badges, meta, err
}

func (g *Gitlab) ProjectBadge(projectId, badgeId string) (*Badge, *ResponseMeta, error) {
func (g *Gitlab) ProjectBadge(projectId string, badgeId int) (*Badge, *ResponseMeta, error) {
u := g.ResourceUrl(ProjectBadgeApiPath, map[string]string{
":id": projectId,
":badge_id": badgeId,
":badge_id": strconv.Itoa(badgeId),
})

badge := new(Badge)
Expand Down
36 changes: 36 additions & 0 deletions gitlab/badges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gitlab

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestProjectBadges(t *testing.T) {
ts, gitlab := mockServerFromMapping(t, "badges/project_1_badges.json")
defer ts.Close()

badges, meta, err := gitlab.ProjectBadges("1", nil)

assert.NoError(t, err)

assert.Equal(t, 5, len(badges))

assert.IsType(t, new(ResponseMeta), meta)
assert.Equal(t, 1, meta.Page)
assert.Equal(t, 10, meta.PerPage)
}

func TestProjectBadge(t *testing.T) {
ts, gitlab := mockServerFromMapping(t, "badges/project_1_badge_1.json")
defer ts.Close()

badge, meta, err := gitlab.ProjectBadge("1", 1)

assert.NoError(t, err)

assert.IsType(t, new(Badge), badge)
assert.Equal(t, 1, badge.Id)

assert.IsType(t, new(ResponseMeta), meta)
}
22 changes: 22 additions & 0 deletions gitlab/branches_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gitlab

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestProjectBranches(t *testing.T) {
ts, gitlab := mockServerFromMapping(t, "branches/project_1_branches.json")
defer ts.Close()

branches, meta, err := gitlab.ProtectedBranches("1", nil)

assert.NoError(t, err)

assert.Equal(t, 10, len(branches))

assert.IsType(t, new(ResponseMeta), meta)
assert.Equal(t, 1, meta.Page)
assert.Equal(t, 10, meta.PerPage)
}
8 changes: 4 additions & 4 deletions gitlab/deploy_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Parameters:
id The ID of a project
*/
func (g *Gitlab) ProjectDeployKeys(id string) ([]*PublicKey, *ResponseMeta, error) {
func (g *Gitlab) ProjectDeployKeys(id string) ([]*SshKey, *ResponseMeta, error) {
u := g.ResourceUrl(ProjectDeployKeysApiPath, map[string]string{":id": id})

var deployKeys []*PublicKey
var deployKeys []*SshKey

contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
Expand All @@ -44,13 +44,13 @@ Parameters:
keyId The ID of a key
*/
func (g *Gitlab) ProjectDeployKey(id, keyId string) (*PublicKey, *ResponseMeta, error) {
func (g *Gitlab) ProjectDeployKey(id, keyId string) (*SshKey, *ResponseMeta, error) {
u := g.ResourceUrl(ProjectDeployKeyApiPath, map[string]string{
":id": id,
":key_id": keyId,
})

var deployKey *PublicKey
var deployKey *SshKey

contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
Expand Down
14 changes: 11 additions & 3 deletions gitlab/stale_tests/groups_test.go → gitlab/groups_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stale_tests
package gitlab

import (
"testing"
Expand All @@ -7,19 +7,26 @@ import (
)

func TestGroups(t *testing.T) {
ts, gitlab := Stub("stubs/groups/index.json")
ts, gitlab := mockServerFromMapping(t, "groups/groups.json")
defer ts.Close()

groups, err := gitlab.Groups()
groups, meta, err := gitlab.Groups(nil)

assert.NoError(t, err)

assert.Equal(t, len(groups), 2)
assert.Equal(t, groups[0].Id, 1)
assert.Equal(t, groups[0].Name, "Foobar Group")
assert.Equal(t, groups[0].Path, "foo-bar")
assert.Equal(t, groups[1].ParentId, 1)
assert.Equal(t, groups[1].FullPath, "foo-bar/another")

assert.IsType(t, new(ResponseMeta), meta)
assert.Equal(t, 1, meta.Page)
assert.Equal(t, 10, meta.PerPage)
}

/*
func TestGroup(t *testing.T) {
ts, gitlab := Stub("stubs/groups/show.json")
defer ts.Close()
Expand Down Expand Up @@ -110,3 +117,4 @@ func TestGroupMembers(t *testing.T) {
assert.Equal(t, members[1].State, "active")
assert.Equal(t, members[1].CreatedAt, "2012-10-22T14:13:35Z")
}
*/
Loading

0 comments on commit 43579c8

Please sign in to comment.