Example #1
0
// CreateTestFeature creates a random feature to be used during testing.
func CreateTestFeature(t *testing.T) *models.Feature {
	s, err := store.NewMySQLStore(
		os.Getenv("LAIKA_MYSQL_USERNAME"),
		os.Getenv("LAIKA_MYSQL_PASSWORD"),
		os.Getenv("LAIKA_MYSQL_HOST"),
		os.Getenv("LAIKA_MYSQL_PORT"),
		os.Getenv("LAIKA_MYSQL_DBNAME"),
	)
	require.NoError(t, err)

	env, err := s.GetEnvironmentByName("test")
	if err != nil {
		env = &models.Environment{
			Name: "test",
		}
		err = s.CreateEnvironment(env)
		require.NoError(t, err)
	}

	feature := &models.Feature{
		Name: "test_feature" + store.Token(),
		Status: map[string]bool{
			"test": true,
		},
	}

	err = s.CreateFeature(feature)
	require.NoError(t, err)

	return feature
}
Example #2
0
// NewTestServer creates a new initialised Laika httptest.Server. The server
// root credentials are "root" as username and password. It contains an
// environment named "test" with an enabled featured named "test_feature",
// and a user whose username is "user" and password is "password".
func NewTestServer(t *testing.T) *httptest.Server {
	s, err := store.NewMySQLStore(
		os.Getenv("LAIKA_MYSQL_USERNAME"),
		os.Getenv("LAIKA_MYSQL_PASSWORD"),
		os.Getenv("LAIKA_MYSQL_HOST"),
		os.Getenv("LAIKA_MYSQL_PORT"),
		os.Getenv("LAIKA_MYSQL_DBNAME"),
	)
	require.NoError(t, err)

	err = s.Ping()
	require.NoError(t, err)

	err = s.Migrate()
	require.NoError(t, err)

	user := models.User{
		Username:     "******" + store.Token(),
		PasswordHash: "awesome_password",
	}
	err = s.CreateUser(&user)
	require.NoError(t, err)

	server, err := NewServer(ServerConfig{
		Store:        s,
		RootUsername: "******",
		RootPassword: "******",
	})
	require.NoError(t, err)

	return httptest.NewServer(server)
}
Example #3
0
func TestUpdateFeature(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	name := "awesome_feature" + store.Token()

	err := client.post("/api/features", &models.Feature{
		Name: name,
	}, nil)
	require.NoError(t, err)

	newName := "not_awesome_feature" + store.Token()

	found := new(models.Feature)
	err = client.patch("/api/features/"+name, &models.Feature{
		Name: newName,
	}, found)
	require.NoError(t, err)

	require.Equal(t, newName, found.Name)
}
func TestUpdateEnvironment(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	name := "prod" + store.Token()

	err := client.post("/api/environments", &models.Feature{
		Name: name,
	}, nil)
	require.NoError(t, err)

	newName := "not_prod" + store.Token()

	found := new(models.Environment)
	err = client.patch("/api/environments/"+name, &models.Feature{
		Name: newName,
	}, found)
	require.NoError(t, err)

	require.Equal(t, newName, found.Name)
}
func TestCreateEnvironment(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	name := "prod" + store.Token()

	found := new(models.Environment)
	err := client.post("/api/environments", &models.Feature{
		Name: name,
	}, found)
	require.NoError(t, err)

	require.NotEqual(t, 0, found.ID)
	require.Equal(t, name, found.Name)
}
Example #6
0
func TestCreateFeature(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	name := "awesome_feature" + store.Token()

	found := new(models.Feature)
	err := client.post("/api/features", &models.Feature{
		Name: name,
	}, found)
	require.NoError(t, err)

	require.NotEqual(t, 0, found.ID)
	require.Equal(t, name, found.Name)
}
Example #7
0
func TestListFeatures(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	for i := 0; i < 10; i++ {
		err := client.post("/api/features", &models.Feature{
			Name: "awesome_feature" + store.Token(),
		}, nil)
		require.NoError(t, err)
	}

	found := []models.Feature{}
	err := client.get("/api/features", &found)
	require.NoError(t, err)

	require.NotEqual(t, len(found), 0)
}
Example #8
0
func TestCreateUser(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	username := "******" + store.Token()

	found := new(models.User)
	err := client.post("/api/users", &models.User{
		Username: username,
		Password: "******",
	}, found)
	require.NoError(t, err)

	require.NotEqual(t, 0, found.ID)
	require.Equal(t, username, found.Username)
	require.Equal(t, "", found.Password)
}
func TestListEnvironment(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	for i := 0; i < 10; i++ {
		err := client.post("/api/environments", &models.Feature{
			Name: "prod_" + store.Token(),
		}, nil)
		require.NoError(t, err)
	}

	found := []models.Environment{}
	err := client.get("/api/environments", &found)
	require.NoError(t, err)

	// ten created plus one that is always present in the test server
	require.NotEqual(t, len(found), 0)
}
Example #10
0
func TestGetUserByUsername(t *testing.T) {
	client := NewTestClient(t, "root", "root")
	defer client.Close()

	username := "******" + store.Token()

	err := client.post("/api/users", &models.User{
		Username: username,
		Password: "******",
	}, nil)
	require.NoError(t, err)

	found := new(models.User)
	err = client.get("/api/users/"+username, found)

	require.NoError(t, err)
	require.Equal(t, username, found.Username)
}