File: group_import_export_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-api-client-go 0.123.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,356 kB
  • sloc: makefile: 17
file content (88 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gitlab

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"reflect"
	"testing"
)

func TestGroupScheduleExport(t *testing.T) {
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/groups/1/export",
		func(w http.ResponseWriter, r *http.Request) {
			testMethod(t, r, http.MethodPost)
			fmt.Fprint(w, `{"message": "202 Accepted"}`)
		})

	_, err := client.GroupImportExport.ScheduleExport(1)
	if err != nil {
		t.Errorf("GroupImportExport.ScheduleExport returned error: %v", err)
	}
}

func TestGroupExportDownload(t *testing.T) {
	mux, client := setup(t)
	content := []byte("fake content")

	mux.HandleFunc("/api/v4/groups/1/export/download",
		func(w http.ResponseWriter, r *http.Request) {
			testMethod(t, r, http.MethodGet)
			w.Write(content)
		})

	export, _, err := client.GroupImportExport.ExportDownload(1)
	if err != nil {
		t.Errorf("GroupImportExport.ExportDownload returned error: %v", err)
	}

	data, err := io.ReadAll(export)
	if err != nil {
		t.Errorf("Error reading export: %v", err)
	}

	want := []byte("fake content")
	if !reflect.DeepEqual(want, data) {
		t.Errorf("GroupImportExport.GroupExportDownload returned %+v, want %+v", data, want)
	}
}

func TestGroupImport(t *testing.T) {
	mux, client := setup(t)

	content := []byte("temporary file's content")
	tmpfile, err := os.CreateTemp(os.TempDir(), "example.*.tar.gz")
	if err != nil {
		tmpfile.Close()
		t.Fatal(err)
	}
	if _, err := tmpfile.Write(content); err != nil {
		tmpfile.Close()
		t.Fatal(err)
	}
	if err := tmpfile.Close(); err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name()) // clean up

	mux.HandleFunc("/api/v4/groups/import",
		func(w http.ResponseWriter, r *http.Request) {
			testMethod(t, r, http.MethodPost)
			fmt.Fprint(w, `{"message": "202 Accepted"}`)
		})

	opt := &GroupImportFileOptions{
		Name:     Ptr("test"),
		Path:     Ptr("path"),
		File:     Ptr(tmpfile.Name()),
		ParentID: Ptr(1),
	}

	_, err = client.GroupImportExport.ImportFile(opt)
	if err != nil {
		t.Errorf("GroupImportExport.ImportFile returned error: %v", err)
	}
}