示例#1
0
func TestDelete(t *testing.T) {
	log.InitWithWriter(nil)
	log.PrintLevel = 9002
	cases := []test{{
		action: "GET", path: "/delete", assert: defaultAssert,
		request:  "",
		status:   http.StatusMethodNotAllowed,
		expected: nil,
		config:   &data.Configuration{},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusBadRequest,
		expected: nil,
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"image-name\":\"fakeImage\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusUnauthorized,
		expected: &GenericResponse{Success: false, Status: "invalid-authtoken"},
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{authTokenError: errors.New("fakeError")},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"image-name\":\"fakeImage\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusForbidden,
		expected: &GenericResponse{Success: false, Status: "no-permissions"},
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{queryImage: data.ImageEntry{ImageName: "image", Format: "png", Adder: "fakeUser2"}},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"image-name\":\"fakeImage\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusNotFound,
		expected: &GenericResponse{Success: false, Status: "not-found"},
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{queryError: errors.New("asd")},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"image-name\":\"fakeImage\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusInternalServerError,
		expected: nil,
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{queryImage: data.ImageEntry{ImageName: "image", Format: "png", Adder: "fakeUser"}, removeError: errors.New("fakeError")},
	}, {
		action: "POST", path: "/delete", assert: defaultAssert,
		request:  "{\"image-name\":\"fakeImage\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusAccepted,
		expected: &GenericResponse{Success: true, Status: "deleted"},
		config:   &data.Configuration{ImageLocation: "/"},
		auth:     fakeAuth{},
		database: fakeDatabase{queryImage: data.ImageEntry{ImageName: "image", Format: "png", Adder: "fakeUser"}},
	}}

	/*{ TODO: Create a test case that makes os.Remove throw an error other than no such file or directory.
		request:  "{\"image-name\":\"fake/Image\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusInternalServerError,
		expected: nil,
		config:   &data.Configuration{ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{imageOwner: "fakeUser"},
	},*/

	for index, c := range cases {
		run(index+1, c, t)
	}
}
示例#2
0
func TestInsert(t *testing.T) {
	log.InitWithWriter(nil)
	log.PrintLevel = 9002
	cases := []test{{
		action: "GET", path: "/insert", assert: defaultAssert,
		request:  "",
		status:   http.StatusMethodNotAllowed,
		expected: nil,
		config:   &data.Configuration{},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{}",
		status:   http.StatusBadRequest,
		expected: nil,
		config:   &data.Configuration{},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\"}",
		status:   http.StatusCreated,
		expected: &GenericResponse{Success: true, Status: "created"},
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\"}",
		status:   http.StatusUnauthorized,
		expected: &GenericResponse{Success: false, Status: "not-logged-in"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusUnauthorized,
		expected: &GenericResponse{Success: false, Status: "invalid-authtoken"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{authTokenError: errors.New("fakeError")},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusForbidden,
		expected: &GenericResponse{Success: false, Status: "already-exists"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{imageOwner: "fakeUser2"},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\"}",
		status:   http.StatusForbidden,
		expected: &GenericResponse{Success: false, Status: "already-exists"},
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{imageOwner: "fakeUser"},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"ZmFrZUltYWdlDQo=\"}",
		status:   http.StatusUnsupportedMediaType,
		expected: &GenericResponse{Success: false, Status: "invalid-mime"},
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"image-name\":\"as>?¿d/das\"}",
		status:   http.StatusInternalServerError,
		expected: nil,
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"totallyBase64\"}",
		status:   http.StatusUnsupportedMediaType,
		expected: &GenericResponse{Success: false, Status: "invalid-image-encoding"},
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusCreated,
		expected: &GenericResponse{Success: true, Status: "created"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\"}",
		status:   http.StatusInternalServerError,
		expected: &GenericResponse{Success: false, Status: "database-error"},
		config:   &data.Configuration{RequireAuth: false, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{insertError: errors.New("fakeError")},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusAccepted,
		expected: &GenericResponse{Success: true, Status: "replaced"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{imageOwner: "fakeUser"},
	}, {
		action: "POST", path: "/insert", assert: defaultAssert,
		request:  "{\"image\": \"" + image + "\",\"username\": \"fakeUser\",\"auth-token\": \"fakeAuthToken\"}",
		status:   http.StatusInternalServerError,
		expected: &GenericResponse{Success: false, Status: "database-error"},
		config:   &data.Configuration{RequireAuth: true, ImageLocation: "/tmp"},
		auth:     fakeAuth{},
		database: fakeDatabase{updateError: errors.New("fakeError"), imageOwner: "fakeUser"},
	}}

	for index, c := range cases {
		run(index+1, c, t)
	}
}