示例#1
0
文件: admin.go 项目: Jyggafey/drone
func AdminSettingsUpdate(w http.ResponseWriter, r *http.Request, u *User) error {
	// get settings from database
	settings := database.SettingsMust()

	// update smtp settings
	settings.Domain = r.FormValue("Domain")
	settings.Scheme = r.FormValue("Scheme")

	// update bitbucket settings
	settings.BitbucketKey = r.FormValue("BitbucketKey")
	settings.BitbucketSecret = r.FormValue("BitbucketSecret")

	// update github settings
	settings.GitHubKey = r.FormValue("GitHubKey")
	settings.GitHubSecret = r.FormValue("GitHubSecret")
	settings.GitHubDomain = r.FormValue("GitHubDomain")
	settings.GitHubApiUrl = r.FormValue("GitHubApiUrl")

	// update gitlab settings
	settings.GitlabApiUrl = r.FormValue("GitlabApiUrl")
	glUrl, err := url.Parse(settings.GitlabApiUrl)
	if err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}
	settings.GitlabDomain = glUrl.Host

	// update smtp settings
	settings.SmtpServer = r.FormValue("SmtpServer")
	settings.SmtpPort = r.FormValue("SmtpPort")
	settings.SmtpAddress = r.FormValue("SmtpAddress")
	settings.SmtpUsername = r.FormValue("SmtpUsername")
	settings.SmtpPassword = r.FormValue("SmtpPassword")

	settings.OpenInvitations = (r.FormValue("OpenInvitations") == "on")

	// validate user input
	if err := settings.Validate(); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}

	// persist changes
	if err := database.SaveSettings(settings); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}

	// make sure the mail package is updated with the
	// latest client information.
	//mail.SetClient(&mail.SMTPClient{
	//	Host: settings.SmtpServer,
	//	Port: settings.SmtpPort,
	//	User: settings.SmtpUsername,
	//	Pass: settings.SmtpPassword,
	//	From: settings.SmtpAddress,
	//})

	return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
}
示例#2
0
func TestGetSettings(t *testing.T) {
	Setup()
	defer Teardown()

	// even though no settings exist yet, we should
	// not see an error since we supress the msg
	settings, err := database.GetSettings()
	//if err != nil {
	//	t.Error(err)
	//}

	// add some settings
	//settings := &modelSettings{}
	settings.Scheme = "https"
	settings.Domain = "foo.com"
	settings.BitbucketKey = "bitbucketkey"
	settings.BitbucketSecret = "bitbucketsecret"
	settings.GitHubKey = "githubkey"
	settings.GitHubSecret = "githubsecret"
	settings.SmtpAddress = "*****@*****.**"
	settings.SmtpServer = "0.0.0.0"
	settings.SmtpUsername = "******"
	settings.SmtpPassword = "******"

	// save the updated settings
	if err := database.SaveSettings(settings); err != nil {
		t.Error(err)
	}

	// re-retrieve the settings post-save
	settings, err = database.GetSettings()
	if err != nil {
		t.Error(err)
	}

	if settings.ID != 1 {
		t.Errorf("Exepected ID %d, got %d", 1, settings.ID)
	}

	if settings.Scheme != "https" {
		t.Errorf("Exepected Scheme %s, got %s", "https", settings.Scheme)
	}

	if settings.Domain != "foo.com" {
		t.Errorf("Exepected Domain %s, got %s", "foo.com", settings.Domain)
	}

	// Verify caching works and is threadsafe
	settingsA, _ := database.GetSettings()
	settingsB, _ := database.GetSettings()
	settingsA.Domain = "foo.bar.baz"
	if settingsA.Domain == settingsB.Domain {
		t.Errorf("Exepected Domain ThreadSafe and unchanged")
	}
}
示例#3
0
文件: admin.go 项目: Jyggafey/drone
func InstallPost(w http.ResponseWriter, r *http.Request) error {
	// we can only perform the inital installation if no
	// users exist in the system
	if users, err := database.ListUsers(); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	} else if len(users) != 0 {
		// if users exist in the systsem
		// we should render a NotFound page
		return RenderNotFound(w)
	}

	// set the email and name
	user := NewUser(r.FormValue("name"), r.FormValue("email"))
	user.Admin = true

	// set the new password
	if err := user.SetPassword(r.FormValue("password")); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}

	// verify fields are correct
	if err := user.Validate(); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}

	// save to the database
	if err := database.SaveUser(user); err != nil {
		return RenderError(w, err, http.StatusBadRequest)
	}

	// update settings
	settings := Settings{}
	settings.Domain = r.FormValue("Domain")
	settings.Scheme = r.FormValue("Scheme")
	settings.GitHubApiUrl = "https://api.github.com"
	settings.GitHubDomain = "github.com"
	settings.GitlabApiUrl = "https://gitlab.com"
	settings.GitlabDomain = "gitlab.com"
	database.SaveSettings(&settings)

	// add the user to the session object
	// so that he/she is loggedin
	SetCookie(w, r, "_sess", user.Email)

	// send the user to the settings page
	// to complete the configuration.
	http.Redirect(w, r, "/account/admin/settings", http.StatusSeeOther)
	return nil
}
示例#4
0
func SetupFixtures() {
	dbtest.Setup()

	// test server
	mux = http.NewServeMux()
	server = httptest.NewServer(mux)
	url, _ := url.Parse(server.URL)

	// set database to use a localhost url for GitHub
	settings := model.Settings{}
	settings.GitHubKey = "123"
	settings.GitHubSecret = "abc"
	settings.GitHubApiUrl = url.String() // normall would be "https://api.github.com"
	settings.GitHubDomain = url.Host     // normally would be "github.com"
	settings.Scheme = url.Scheme
	settings.Domain = "localhost"
	database.SaveSettings(&settings)

	// -----------------------------------------------------------------------------------
	// fixture to return a public repository and successfully
	// create a commit hook.

	mux.HandleFunc("/repos/example/public", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, `{
			"name": "public",
			"full_name": "example/public",
			"private": false
		}`)
	})

	mux.HandleFunc("/repos/example/public/hooks", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"url": "https://api.github.com/repos/example/public/hooks/1",
			"name": "web",
			"events": [ "push", "pull_request" ],
			"id": 1
		}`)
	})

	// -----------------------------------------------------------------------------------
	// fixture to return a private repository and successfully
	// create a commit hook and ssh deploy key

	mux.HandleFunc("/repos/example/private", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, `{
			"name": "private",
			"full_name": "example/private",
			"private": true
		}`)
	})

	mux.HandleFunc("/repos/example/private/hooks", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"url": "https://api.github.com/repos/example/private/hooks/1",
			"name": "web",
			"events": [ "push", "pull_request" ],
			"id": 1
		}`)
	})

	mux.HandleFunc("/repos/example/private/keys", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"id": 1,
			"key": "ssh-rsa AAA...",
			"url": "https://api.github.com/user/keys/1",
			"title": "octocat@octomac"
		}`)
	})

	// -----------------------------------------------------------------------------------
	// fixture to return a not found when accessing a github repository.

	mux.HandleFunc("/repos/example/notfound", func(w http.ResponseWriter, r *http.Request) {
		http.NotFound(w, r)
	})

	// -----------------------------------------------------------------------------------
	// fixture to return a public repository and successfully
	// create a commit hook.

	mux.HandleFunc("/repos/example/hookerr", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, `{
			"name": "hookerr",
			"full_name": "example/hookerr",
			"private": false
		}`)
	})

	mux.HandleFunc("/repos/example/hookerr/hooks", func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Forbidden", http.StatusForbidden)
	})

	// -----------------------------------------------------------------------------------
	// fixture to return a private repository and successfully
	// create a commit hook and ssh deploy key

	mux.HandleFunc("/repos/example/keyerr", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, `{
			"name": "keyerr",
			"full_name": "example/keyerr",
			"private": true
		}`)
	})

	mux.HandleFunc("/repos/example/keyerr/hooks", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"url": "https://api.github.com/repos/example/keyerr/hooks/1",
			"name": "web",
			"events": [ "push", "pull_request" ],
			"id": 1
		}`)
	})

	mux.HandleFunc("/repos/example/keyerr/keys", func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Forbidden", http.StatusForbidden)
	})

	// -----------------------------------------------------------------------------------
	// fixture to return a public repository and successfully to
	// test adding a team.

	mux.HandleFunc("/repos/example/team", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, `{
			"name": "team",
			"full_name": "example/team",
			"private": false
		}`)
	})

	mux.HandleFunc("/repos/example/team/hooks", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"url": "https://api.github.com/repos/example/team/hooks/1",
			"name": "web",
			"events": [ "push", "pull_request" ],
			"id": 1
		}`)
	})
}