func TestListLibraries(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	l1 := testLibrary("l1", "/tmp/l1")
	err := database.SaveLibrary(l1)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	l2 := testLibrary("l2", "/tmp/l2")
	err = database.SaveLibrary(l2)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := http.Get(s.URL + "/api/libraries")
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var libs []Library
	err = json.NewDecoder(r.Body).Decode(&libs)
	if err != nil {
		t.Errorf("unparsable response: %v", err)
	}

	if len(libs) != 2 {
		t.Errorf("expected 2 libraries, got %d", len(libs))
	}
}
func TestDeleteLibrary(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	l1 := testLibrary("l1", "/tmp/l1")
	err := database.SaveLibrary(l1)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := Delete(s.URL + "/api/libraries/" + strconv.FormatInt(l1.ID, 10))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Fatalf("expected status code 200, got %d", r.StatusCode)
	}

	l, err := database.GetLibrary(l1.ID)
	if err == nil {
		t.Fatalf("expected no results, got %v", l)
	}
}
Exemple #3
0
func TestProcessLibrary(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	err := os.MkdirAll("/tmp/lib1", 0755)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	defer os.RemoveAll("/tmp/lib1")

	_, err = os.Create("/tmp/lib1/Sintel (2013).mkv")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	l := testLibrary("l1", "/tmp/lib1")
	err = database.SaveLibrary(l)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	r, err := http.Get(fmt.Sprintf("%s/api/libraries/%d/process", s.URL, l.ID))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Fatalf("expected statusCode 200, got %d", r.StatusCode)
	}
}
Exemple #4
0
func LibraryCreate(w http.ResponseWriter, r *http.Request) error {
	var library Library
	err := json.NewDecoder(r.Body).Decode(&library)
	if err != nil {
		return err
	}

	err = database.SaveLibrary(&library)
	if err != nil {
		return err
	}

	http.Redirect(w, r, r.URL.Path, http.StatusSeeOther)

	return nil
}
Exemple #5
0
// PUT /libraries
// Creates a new library and returns it
func LibraryCreate(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	var l Library
	err := json.NewDecoder(r.Body).Decode(&l)
	if err != nil {
		return nil, err
	}

	if l.ID != 0 {
		return nil, badRequest{errors.New("Library already has an ID")}
	}

	err = database.SaveLibrary(&l)
	if err != nil {
		return nil, err
	}

	return l, nil
}
func TestShowLibrary(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	l1 := testLibrary("l1", "/tmp/l1")
	err := database.SaveLibrary(l1)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := http.Get(s.URL + "/api/libraries/" + strconv.FormatInt(l1.ID, 10))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Fatalf("expected status code 200, got %d", r.StatusCode)
	}

	var resp Library
	err = json.NewDecoder(r.Body).Decode(&resp)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if resp.ID != l1.ID {
		t.Fatalf("expected library id %d, got %d", l1.ID, resp.ID)
	}

	if resp.Name != l1.Name {
		t.Fatalf("expected library name %q, got %q", l1.Name, resp.Name)
	}

	if resp.Path != l1.Path {
		t.Fatalf("expected library path %q, got %q", l1.Path, resp.Path)
	}

}