remove old test files
Former-commit-id: f84bd9945948105adbad9e1adec4cc072fc3a083 [formerly f6f884ae5c22aa3f51ffffe7828a892a09b8c285] [formerly edac9defd87c75efb7fbb86a5fd6aaa2084bbd10 [formerly 0117cff0e6]]
Former-commit-id: e5093a9010603aff9432dcc332e7de3507b8ec38 [formerly 95474b25e1c766a94c32ec69136f261bd0d908b1]
Former-commit-id: 0029467a0059d12c2dbf19ac0339a4bc6b1185b6
			
			
				pull/726/head
			
			
		
							parent
							
								
									d5cefa20b3
								
							
						
					
					
						commit
						9186c1f36c
					
				|  | @ -1,49 +0,0 @@ | |||
| package filemanager | ||||
| 
 | ||||
| import ( | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/hacdias/fileutils" | ||||
| ) | ||||
| 
 | ||||
| type test struct { | ||||
| 	*FileManager | ||||
| 	Temp string | ||||
| } | ||||
| 
 | ||||
| func (t test) Clean() { | ||||
| 	t.db.Close() | ||||
| 	os.RemoveAll(t.Temp) | ||||
| } | ||||
| 
 | ||||
| func newTest(t *testing.T) *test { | ||||
| 	temp, err := ioutil.TempDir("", t.Name()) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Error creating temporary directory: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	scope := filepath.Join(temp, "scope") | ||||
| 	database := filepath.Join(temp, "database.db") | ||||
| 
 | ||||
| 	err = fileutils.CopyDir("./testdata", scope) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Error copying the test data: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	user := DefaultUser | ||||
| 	user.FileSystem = fileutils.Dir(scope) | ||||
| 
 | ||||
| 	fm, err := New(database, user) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Error creating a file manager instance: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	return &test{ | ||||
| 		FileManager: fm, | ||||
| 		Temp:        temp, | ||||
| 	} | ||||
| } | ||||
|  | @ -1,92 +0,0 @@ | |||
| package http | ||||
| 
 | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| var defaultCredentials = "{\"username\":\"admin\",\"password\":\"admin\"}" | ||||
| 
 | ||||
| var authHandlerTests = []struct { | ||||
| 	Data     string | ||||
| 	Expected int | ||||
| }{ | ||||
| 	{defaultCredentials, http.StatusOK}, | ||||
| 	{"{\"username\":\"admin\",\"password\":\"wrong\"}", http.StatusForbidden}, | ||||
| 	{"{\"username\":\"wrong\",\"password\":\"admin\"}", http.StatusForbidden}, | ||||
| } | ||||
| 
 | ||||
| func TestAuthHandler(t *testing.T) { | ||||
| 	fm := newTest(t) | ||||
| 	defer fm.Clean() | ||||
| 
 | ||||
| 	for _, test := range authHandlerTests { | ||||
| 		req, err := http.NewRequest("POST", "/api/auth/get", strings.NewReader(test.Data)) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 
 | ||||
| 		w := httptest.NewRecorder() | ||||
| 		fm.ServeHTTP(w, req) | ||||
| 
 | ||||
| 		if w.Code != test.Expected { | ||||
| 			t.Errorf("Wrong status code: got %v want %v", w.Code, test.Expected) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestRenewHandler(t *testing.T) { | ||||
| 	fm := newTest(t) | ||||
| 	defer fm.Clean() | ||||
| 
 | ||||
| 	// First, we have to make an auth request to get the user authenticated, | ||||
| 	r, err := http.NewRequest("POST", "/api/auth/get", strings.NewReader(defaultCredentials)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(fm.Err) | ||||
| 	} | ||||
| 
 | ||||
| 	w := httptest.NewRecorder() | ||||
| 	fm.ServeHTTP(w, r) | ||||
| 
 | ||||
| 	if w.Code != http.StatusOK { | ||||
| 		t.Errorf("Couldn't authenticate: got %v", w.Code) | ||||
| 	} | ||||
| 
 | ||||
| 	token := w.Body.String() | ||||
| 
 | ||||
| 	// Test renew authorization via Authorization Header. | ||||
| 	r, err = http.NewRequest("GET", "/api/auth/renew", nil) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(fm.Err) | ||||
| 	} | ||||
| 
 | ||||
| 	r.Header.Set("Authorization", "Bearer "+token) | ||||
| 	w = httptest.NewRecorder() | ||||
| 	fm.ServeHTTP(w, r) | ||||
| 
 | ||||
| 	if w.Code != http.StatusOK { | ||||
| 		t.Errorf("Can't renew auth via header: got %v", w.Code) | ||||
| 	} | ||||
| 
 | ||||
| 	// Test renew authorization via cookie field. | ||||
| 	r, err = http.NewRequest("GET", "/api/auth/renew", nil) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(fm.Err) | ||||
| 	} | ||||
| 
 | ||||
| 	r.AddCookie(&http.Cookie{ | ||||
| 		Value:   token, | ||||
| 		Name:    "auth", | ||||
| 		Expires: time.Now().Add(1 * time.Hour), | ||||
| 	}) | ||||
| 
 | ||||
| 	w = httptest.NewRecorder() | ||||
| 	fm.ServeHTTP(w, r) | ||||
| 
 | ||||
| 	if w.Code != http.StatusOK { | ||||
| 		t.Errorf("Can't renew auth via cookie: got %v", w.Code) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	 Henrique Dias
						Henrique Dias