Skip to content

Commit

Permalink
Add debug flag; Add check for Org/Team name
Browse files Browse the repository at this point in the history
  • Loading branch information
conortm committed May 19, 2015
1 parent 7353387 commit 070743e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
15 changes: 3 additions & 12 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,14 @@ func newGithubClient(token string) *githubClient {
TokenType: "token",
},
}))

/* DEBUG: print rate-limit.
fmt.Println("<DEBUG>")
rate, _, err := client.RateLimit()
if err != nil {
fmt.Printf("Error fetching GitHub API Rate Limit: %#v\n", err)
} else {
fmt.Printf("GitHub API Rate Limit: %#v\n", rate)
}
fmt.Println("</DEBUG>")
// END DEBUG. */

return &githubClient{client}
}

func (gc *githubClient) getTeamID(orgName string) (int, error) {
orgNameArray := strings.Split(orgName, "/")
if len(orgNameArray) != 2 {
return 0, errors.New("Team name must be in the form: MyOrg/Team Name")
}
org := orgNameArray[0]
name := orgNameArray[1]
opt := &github.ListOptions{PerPage: 25}
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Pass a single 'username' argument to only print/write keys for that user.

var (
configFilename = flag.String("config", "config.yml", "Path to yaml config file")
debug = flag.Bool("d", false, "Add debugging output")
writeToFile = flag.Bool("write", false, "Write keys to users' authorized_keys files")
)

Expand Down Expand Up @@ -81,6 +82,12 @@ func main() {

client := newGithubClient(config.GithubToken)

if *debug {
if rate, _, err := client.RateLimit(); err == nil {
fmt.Printf("GitHub API Rate Limit: %#v\n", rate)
}
}

usernamesKeys := getUsernamesKeys(config, client, flag.Arg(0))

for username, keys := range usernamesKeys {
Expand Down
25 changes: 10 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ func teardown() {
}

func TestConfig(t *testing.T) {
// Test that testdata/config.yml jives with config struct
// Test that test config file jives with config struct
testConfig, err := newConfig(testdataConfigFilename)

assert.Nil(t, err)
assert.Equal(t, "my_github_token", testConfig.GithubToken)
assert.Len(t, testConfig.Users, 2)
Expand All @@ -70,7 +69,7 @@ func TestConfig(t *testing.T) {
assert.Equal(t, "MyOrg/Team 1", testConfig.Users[0].GithubTeams[0])

// Test nonexistent config file error
_, err = newConfig("thisfileshouldnotexist.yml")
_, err = newConfig("testdata/thisfileshouldnotexist.yml")
assert.Error(t, err)

// Test malformed yaml error
Expand All @@ -86,14 +85,17 @@ func TestGetTeamID(t *testing.T) {
setup()
defer teardown()

// Test valid Team ID
// Test Team ID that exists
teamID, err := client.getTeamID("MyOrg/Team 2")

assert.Nil(t, err)
assert.Equal(t, 2, teamID)

// Test invalid Team ID
_, err = client.getTeamID("MyOrg/Invalid Team")
_, err = client.getTeamID("Invalid Team Name")
assert.Error(t, err)

// Test Team ID that does not exist
_, err = client.getTeamID("MyOrg/Nonexistant Team")
assert.Error(t, err)
}

Expand All @@ -103,7 +105,6 @@ func TestGetMembersOfTeam(t *testing.T) {

// Test getting members of valid Team
membersOfTeam, err := client.getMembersOfTeam("MyOrg/Team 2")

assert.Nil(t, err)
assert.Len(t, membersOfTeam, 1)
assert.Equal(t, "github_user_3", membersOfTeam[0])
Expand All @@ -118,7 +119,6 @@ func TestGetKeysOfUser(t *testing.T) {
defer teardown()

keysOfUser, err := client.getKeysOfUser("github_user_1")

assert.Nil(t, err)
assert.Len(t, keysOfUser, 2)
assert.Equal(t, "github_user_1_key_2", keysOfUser[1])
Expand All @@ -131,9 +131,7 @@ func TestGetKeysOfUsersAndTeams(t *testing.T) {
users := []string{"github_user_1"}
teams := []string{"MyOrg/Team 1", "MyOrg/Team 2"}
expectedKeys := []string{"github_user_1_key_1", "github_user_1_key_2", "github_user_2_key_1", "github_user_3_key_1"}

actualKeys := client.getKeysOfUsersAndTeams(users, teams)

assert.Len(t, actualKeys, len(expectedKeys))
for _, expectedKey := range expectedKeys {
assert.Contains(t, actualKeys, expectedKey)
Expand Down Expand Up @@ -168,7 +166,6 @@ func TestGetUsernamesKeys(t *testing.T) {
},
}
actualUsernamesKeys := getUsernamesKeys(config, client, "superadmin")

testUsernamesKeys(t, expectedUsernamesKeys, actualUsernamesKeys)

// Test for all usernames
Expand All @@ -179,7 +176,6 @@ func TestGetUsernamesKeys(t *testing.T) {
"github_user_4_key_1",
}
actualUsernamesKeys = getUsernamesKeys(config, client, "")

testUsernamesKeys(t, expectedUsernamesKeys, actualUsernamesKeys)
}

Expand All @@ -189,20 +185,19 @@ github_user_1_key_2
github_user_1_key_3`
keys := []string{"github_user_1_key_1", "github_user_1_key_2", "github_user_1_key_3"}
actual := getKeysOutput(keys)

assert.Equal(t, expected, actual)
}

func TestWriteKeysToFile(t *testing.T) {
// Test writing test keys to a test file
keys := []string{"github_user_1_key_1", "github_user_1_key_2", "github_user_1_key_3"}

authorizedKeysFile, _ := ioutil.TempFile("", "ghkeys-authorized_keys")
authorizedKeysFilename := authorizedKeysFile.Name()
defer os.RemoveAll(authorizedKeysFilename)
err := writeKeysToFile(keys, authorizedKeysFilename)

assert.Nil(t, err)

// Compare test file to what we expect it to have
expectedKeysFileContent, _ := ioutil.ReadFile(testAuthorizedKeysFilename)
actualKeysFileContent, _ := ioutil.ReadFile(authorizedKeysFilename)
assert.Equal(t, expectedKeysFileContent, actualKeysFileContent)
Expand Down

0 comments on commit 070743e

Please sign in to comment.