// TestUpdateTeam tests the ability to updatee an // existing Team in the database. func TestUpdateTeam(t *testing.T) { Setup() defer Teardown() // get the user we plan to update team, err := database.GetTeam(1) if err != nil { t.Error(err) } // update fields team.Email = "*****@*****.**" team.Gravatar = "61024896f291303615bcd4f7a0dcfb74" // update the database if err := database.SaveTeam(team); err != nil { t.Error(err) } // get the updated team updatedTeam, err := database.GetTeam(1) if err != nil { t.Error(err) } // verify the updated fields if team.Email != updatedTeam.Email { t.Errorf("Exepected Email %s, got %s", team.Email, updatedTeam.Email) } if team.Gravatar != updatedTeam.Gravatar { t.Errorf("Exepected Gravatar %s, got %s", team.Gravatar, updatedTeam.Gravatar) } }
// Create a new Team. func TeamCreate(w http.ResponseWriter, r *http.Request, u *User) error { // set the name and email from the form data team := Team{} team.SetName(r.FormValue("name")) team.SetEmail(r.FormValue("email")) if err := team.Validate(); err != nil { return RenderError(w, err, http.StatusBadRequest) } if err := database.SaveTeam(&team); err != nil { return RenderError(w, err, http.StatusBadRequest) } // add default member to the team (me) if err := database.SaveMember(u.ID, team.ID, RoleOwner); err != nil { return RenderError(w, err, http.StatusInternalServerError) } return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) }
// Update a specific Team. func TeamUpdate(w http.ResponseWriter, r *http.Request, u *User) error { // get team from the database teamName := r.FormValue(":team") team, err := database.GetTeamSlug(teamName) if err != nil { return fmt.Errorf("Forbidden") } if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member { return fmt.Errorf("Forbidden") } team.Name = r.FormValue("name") team.SetEmail(r.FormValue("email")) if err := team.Validate(); err != nil { return RenderError(w, err, http.StatusBadRequest) } if err := database.SaveTeam(team); err != nil { return RenderError(w, err, http.StatusBadRequest) } return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) }
func Setup() { // create an in-memory database database.Init("sqlite3", ":memory:") // create dummy user data user1 := User{ Password: "******", Name: "Brad Rydzewski", Email: "*****@*****.**", Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87", Token: "123", Admin: true} user2 := User{ Password: "******", Name: "Thomas Burke", Email: "*****@*****.**", Gravatar: "c62f7126273f7fa786274274a5dec8ce", Token: "456", Admin: false} user3 := User{ Password: "******", Name: "Carlos Morales", Email: "*****@*****.**", Gravatar: "c2180a539620d90d68eaeb848364f1c2", Token: "789", Admin: false} database.SaveUser(&user1) database.SaveUser(&user2) database.SaveUser(&user3) // create dummy team data team1 := Team{ Slug: "drone", Name: "Drone", Email: "*****@*****.**", Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87"} team2 := Team{ Slug: "github", Name: "Github", Email: "*****@*****.**", Gravatar: "61024896f291303615bcd4f7a0dcfb74"} team3 := Team{ Slug: "golang", Name: "Golang", Email: "*****@*****.**", Gravatar: "991695cc770c6b8354b68cd18c280b95"} database.SaveTeam(&team1) database.SaveTeam(&team2) database.SaveTeam(&team3) // create team membership data database.SaveMember(user1.ID, team1.ID, RoleOwner) database.SaveMember(user2.ID, team1.ID, RoleAdmin) database.SaveMember(user3.ID, team1.ID, RoleWrite) database.SaveMember(user1.ID, team2.ID, RoleOwner) database.SaveMember(user2.ID, team2.ID, RoleAdmin) database.SaveMember(user3.ID, team2.ID, RoleWrite) database.SaveMember(user1.ID, team3.ID, RoleRead) // create dummy repo data repo1 := Repo{ Slug: "github.com/drone/drone", Host: "github.com", Owner: "drone", Name: "drone", Private: true, Disabled: false, SCM: "git", URL: "[email protected]:drone/drone.git", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user1.ID, TeamID: team1.ID, } repo2 := Repo{ Slug: "bitbucket.org/drone/test", Host: "bitbucket.org", Owner: "drone", Name: "test", Private: false, Disabled: false, SCM: "hg", URL: "https://bitbucket.org/drone/test", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user1.ID, TeamID: team1.ID, } repo3 := Repo{ Slug: "bitbucket.org/brydzewski/test", Host: "bitbucket.org", Owner: "brydzewski", Name: "test", Private: false, Disabled: false, SCM: "hg", URL: "https://bitbucket.org/brydzewski/test", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user2.ID, } database.SaveRepo(&repo1) database.SaveRepo(&repo2) database.SaveRepo(&repo3) commit1 := Commit{ RepoID: repo1.ID, Status: "Success", Hash: "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit2 := Commit{ RepoID: repo1.ID, Status: "Failure", Hash: "0eb2fa13e9f4139e803b6ad37831708d4786c74a", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit3 := Commit{ RepoID: repo1.ID, Status: "Failure", Hash: "60a7fe87ccf01d0152e53242528399e05acaf047", Branch: "dev", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit4 := Commit{ RepoID: repo2.ID, Status: "Success", Hash: "a4078d1e9a0842cdd214adbf0512578799a4f2ba", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } // create dummy commit data database.SaveCommit(&commit1) database.SaveCommit(&commit2) database.SaveCommit(&commit3) database.SaveCommit(&commit4) // create dummy build data database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.10", Status: "Success", Duration: 60}) database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.09", Status: "Success", Duration: 70}) database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.10", Status: "Success", Duration: 10}) database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.09", Status: "Failure", Duration: 65}) database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.10", Status: "Failure", Duration: 50}) database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.09", Status: "Failure", Duration: 55}) }