Example #1
0
func initialSetup() {
	cursor, err := db.UserTable.Count().Run(db.Session)
	if err != nil {
		log.Fatal(err)
	}
	var count int
	cursor.One(&count)
	if count == 0 {
		firstUser := models.User{
			Email:    "*****@*****.**",
			Password: "******",
			Groups: map[string]struct{}{
				"admin": {},
			},
		}
		authentication.CreateUser(firstUser)
	}
}
Example #2
0
func TestCreateModpackHandler(t *testing.T) {
	setup()

	user := authentication.UserBody{Email: "*****@*****.**", Password: "******"}
	{
		_, r := authentication.CreateUser(user)
		user.Id = r.User.Id
	}
	_, loginInfo := authentication.LoginUser(user)
	token := loginInfo.Token

	gin.SetMode(gin.TestMode)
	router := gin.Default()
	apiRouter := router.Group("/api")
	GetGroupHandler(apiRouter)
	ts := httptest.NewServer(router)
	defer ts.Close()

	modpacksUrl := fmt.Sprintf("%s/api/modpacks/", ts.URL)

	modpack := Modpack{
		Name: "Test pack",
		Slug: "test-pack",
	}

	modpackJson, _ := json.Marshal(modpack)

	// Test creation without authentication header. It should not be allowed at all.
	request, err := http.NewRequest(http.MethodPost, modpacksUrl, bytes.NewReader(modpackJson))
	if err != nil {
		t.Error(err)
	}

	res, err := http.DefaultClient.Do(request)
	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	response := testutils.ParseResponse(res)
	if response.StatusCode != http.StatusUnauthorized {
		t.Error("Expcted status", http.StatusUnauthorized, ", got", response.StatusCode)
	}

	// Test creation of mod with correct authentication headers
	request, err = http.NewRequest(http.MethodPost, modpacksUrl, bytes.NewReader(modpackJson))
	if err != nil {
		t.Error(err)
	}
	request.Header.Add("Authorization", "Bearer "+token)

	res, err = http.DefaultClient.Do(request)
	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	response = testutils.ParseResponse(res)
	if response.StatusCode != http.StatusOK {
		t.Error("Expected status", http.StatusOK, ", got", response.StatusCode)
	}

	var newModpack Modpack
	mapstructure.Decode(response.Body, &newModpack)
	if newModpack.Id == modpack.Id {
		t.Error("Expected the new modpack to have gotten an id. It didn't.")
	}

	if newModpack.Name != modpack.Name {
		t.Error("Modpack name was not set correctly.")
	}
	if newModpack.Slug != modpack.Slug {
		t.Error("Modpack slug was not set correctly.")
	}

	// Test getting the modpack
	request, err = http.NewRequest(http.MethodGet, modpacksUrl+newModpack.Id, nil)
	if err != nil {
		t.Error(err)
	}
	request.Header.Add("Authorization", "Bearer "+token)

	res, err = http.DefaultClient.Do(request)
	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	response = testutils.ParseResponse(res)
	if response.StatusCode != http.StatusOK {
		t.Error("Expected status code 200, got", response.StatusCode)
	}

}