Skip to content

Commit

Permalink
More tests and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
conortm committed May 1, 2015
1 parent 8db7117 commit 798894c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $ go get github.com/conortm/ghkeys

## Configure

Create a `config.yml` file like:
Create a `config.yml` file like [`config.example.yml`](./config.example.yml):

```yaml
---
Expand Down Expand Up @@ -60,4 +60,3 @@ $ ghkeys -config="/path/to/config.yml" -write
* Implement https://github.com/sourcegraph/apiproxy
* Validate config file
* Better error handling
* Tests
2 changes: 1 addition & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func appendUserIfMissing(users []string, newUser string) []string {
return append(users, newUser)
}

func (gc *githubClient) getKeys(users, teams []string) []string {
func (gc *githubClient) getKeysOfUsersAndTeams(users, teams []string) []string {
var keys []string
// add members of teams to array of users
teamMembersChan := make(chan []string)
Expand Down
50 changes: 29 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,50 @@ type usernameKeys struct {
keys []string
}

func main() {
flag.Usage = usage
flag.Parse()

config, err := newConfig(*configFilename)
check(err)

singleUsername := flag.Arg(0)

// TODO: validate config, including that usernames exist on server.

gc := newGithubClient(config.GithubToken)

func getUsernamesKeys(config config, client *githubClient, singleUsername string) map[string][]string {
usernamesKeys := make(map[string][]string)
usernameCount := 0
usernameKeysChan := make(chan usernameKeys)
for _, user := range config.Users {
if singleUsername == "" || singleUsername == user.Username {
usernameCount++
go func(username string, users, teams []string) {
keys := gc.getKeys(users, teams)
keys := client.getKeysOfUsersAndTeams(users, teams)
usernameKeysChan <- usernameKeys{username: username, keys: keys}
}(user.Username, user.GithubUsers, user.GithubTeams)
}
}
for i := 0; i < usernameCount; i++ {
usernameKey := <-usernameKeysChan
authorizedKeysOutput := strings.Join(usernameKey.keys, "\n")
tempUsernameKeys := <-usernameKeysChan
usernamesKeys[tempUsernameKeys.username] = tempUsernameKeys.keys
}
return usernamesKeys
}

func main() {
flag.Usage = usage
flag.Parse()

config, err := newConfig(*configFilename)
check(err)
// TODO: validate config values

client := newGithubClient(config.GithubToken)

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

for username, keys := range usernamesKeys {
keysOutput := strings.Join(keys, "\n")
if *writeToFile {
authorizedKeysFilename := fmt.Sprintf("/home/%s/.ssh/authorized_keys", usernameKey.username)
f, err := os.Create(authorizedKeysFilename)
// TODO: Is this always the path?
authorizedKeysFilename := fmt.Sprintf("/home/%s/.ssh/authorized_keys", username)
authorizedKeysFile, err := os.Open(authorizedKeysFilename)
check(err)
defer f.Close()
_, err = f.WriteString(authorizedKeysOutput)
defer authorizedKeysFile.Close()
_, err = authorizedKeysFile.WriteString(keysOutput)
check(err)
} else {
fmt.Println(authorizedKeysOutput)
fmt.Println(keysOutput)
}
}
}
52 changes: 47 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,61 @@ func TestGetKeysOfUser(t *testing.T) {
assert.Equal(t, "github_user_1_key_2", keysOfUser[1])
}

func TestGetKeys(t *testing.T) {
func TestGetKeysOfUsersAndTeams(t *testing.T) {
setup()
defer teardown()

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"}

keys := client.getKeys(users, teams)
actualKeys := client.getKeysOfUsersAndTeams(users, teams)

// TODO: Better way to do this?
assert.Len(t, keys, len(expectedKeys))
assert.Len(t, actualKeys, len(expectedKeys))
for _, expectedKey := range expectedKeys {
assert.Contains(t, keys, expectedKey)
assert.Contains(t, actualKeys, expectedKey)
}
}

func testUsernamesKeys(t *testing.T, expectedUsernamesKeys, actualUsernamesKeys map[string][]string) {
assert.Len(t, actualUsernamesKeys, len(expectedUsernamesKeys))
for expectedUsername, expectedKeys := range expectedUsernamesKeys {
actualKeys, ok := actualUsernamesKeys[expectedUsername]
assert.True(t, ok)
for _, expectedKey := range expectedKeys {
assert.Contains(t, actualKeys, expectedKey)
}
}
}

func TestGetUsernamesKeys(t *testing.T) {
setup()
defer teardown()

config, err := newConfig("config.example.yml")
assert.Nil(t, err)

// Test for single username
expectedUsernamesKeys := map[string][]string{
"superadmin": []string{
"github_user_1_key_1",
"github_user_1_key_2",
"github_user_2_key_1",
"github_user_3_key_1",
},
}
actualUsernamesKeys := getUsernamesKeys(config, client, "superadmin")

testUsernamesKeys(t, expectedUsernamesKeys, actualUsernamesKeys)

// Test for all usernames
expectedUsernamesKeys["admin"] = []string{
"github_user_1_key_1",
"github_user_1_key_2",
"github_user_2_key_1",
"github_user_4_key_1",
}
actualUsernamesKeys = getUsernamesKeys(config, client, "")

testUsernamesKeys(t, expectedUsernamesKeys, actualUsernamesKeys)
}

0 comments on commit 798894c

Please sign in to comment.