aboutsummaryrefslogtreecommitdiff
path: root/http_server_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-15 02:22:37 +0700
committerShulhan <ms@kilabit.info>2023-11-16 00:54:55 +0700
commit622bd72e6f2d3bd848963fc486fef1b39760e2ac (patch)
tree476d90b90a906f4222017b2842de8d763367d66f /http_server_test.go
parent79ac55824fc969ec3a773e46e450f6ba410bee1a (diff)
downloadawwan-622bd72e6f2d3bd848963fc486fef1b39760e2ac.tar.xz
all: implement HTTP API to decrypt file
Similar to the CLI, the HTTP API accept the path of vault file and return the path to decrypted file.
Diffstat (limited to 'http_server_test.go')
-rw-r--r--http_server_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/http_server_test.go b/http_server_test.go
index ab036c6..159c166 100644
--- a/http_server_test.go
+++ b/http_server_test.go
@@ -1,3 +1,6 @@
+// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
package awwan
import (
@@ -12,6 +15,94 @@ import (
"github.com/shuLhan/share/lib/test"
)
+func TestHttpServer_Decrypt(t *testing.T) {
+ type testCase struct {
+ desc string
+ expResp string
+ expErr string
+ reqBody []byte
+ }
+
+ var (
+ tdata *test.Data
+ err error
+ )
+
+ tdata, err = test.LoadData(`testdata/http_server/decrypt_test.data`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var aww *Awwan
+
+ aww, err = New(`testdata/encrypt-with-passfile`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var httpd *httpServer
+
+ httpd, err = newHttpServer(aww, ``)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var endpointDecrypt = &libhttp.Endpoint{
+ Method: libhttp.RequestMethodPost,
+ Path: pathAwwanApiDecrypt,
+ RequestType: libhttp.RequestTypeJSON,
+ ResponseType: libhttp.ResponseTypeJSON,
+ }
+
+ var cases = []testCase{{
+ desc: `With valid request`,
+ reqBody: tdata.Input[`withValidRequest/body`],
+ expResp: string(tdata.Output[`withValidRequest/Response/body`]),
+ }, {
+ desc: `With empty value`,
+ reqBody: tdata.Input[`withEmptyValue/body`],
+ expResp: string(tdata.Output[`withEmptyValue/Response/body`]),
+ }, {
+ desc: `With directory`,
+ reqBody: tdata.Input[`withDirectory/body`],
+ expResp: string(tdata.Output[`withDirectory/Response/body`]),
+ }, {
+ desc: `With file not exist`,
+ reqBody: tdata.Input[`withFileNotExist/body`],
+ expResp: string(tdata.Output[`withFileNotExist/Response/body`]),
+ }}
+
+ var (
+ httpReq *http.Request
+ httpRes *http.Response
+ c testCase
+ reqBody bytes.Buffer
+ gotResp bytes.Buffer
+ resp []byte
+ )
+
+ for _, c = range cases {
+ t.Log(c.desc)
+
+ reqBody.Reset()
+ reqBody.Write(c.reqBody)
+
+ httpReq = httptest.NewRequest(http.MethodPost, endpointDecrypt.Path, &reqBody)
+
+ var httpWriter = httptest.NewRecorder()
+
+ httpd.ServeHTTP(httpWriter, httpReq)
+
+ httpRes = httpWriter.Result()
+
+ resp, _ = io.ReadAll(httpRes.Body)
+
+ gotResp.Reset()
+ json.Indent(&gotResp, resp, ``, ` `)
+ test.Assert(t, c.desc, c.expResp, gotResp.String())
+ }
+}
+
func TestHttpServer_Encrypt(t *testing.T) {
type testCase struct {
desc string