From 1391b42b9e2e053d44b12b28427acd329a9894da Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:22:58 +0700 Subject: [PATCH 01/39] Create app test --- app_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app_test.go b/app_test.go index d9e8f7b..16331eb 100644 --- a/app_test.go +++ b/app_test.go @@ -6,13 +6,27 @@ package kintone import ( "bytes" + "crypto/tls" + "fmt" "io/ioutil" + "net" + "net/http" + "net/http/httptest" "os" "strings" "testing" "time" ) +func newAppForTest(domain string, user string, password string, appID uint64) *App { + return &App{ + Domain: domain, + User: user, + Password: password, + AppId: appID, + } +} + func newApp(appID uint64) *App { return &App{ Domain: os.Getenv("KINTONE_DOMAIN"), From ceede49828d94e0b72de609097225104ce1bbe15 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:23:17 +0700 Subject: [PATCH 02/39] create response test json --- app_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app_test.go b/app_test.go index 16331eb..7fc2446 100644 --- a/app_test.go +++ b/app_test.go @@ -44,6 +44,17 @@ func newAppWithApiToken(appId uint64) *App { } } +func createResponseLocalTestServer(data string) (*httptest.Server, error) { + ts, err := NewLocalHTTPSTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, data) + })) + if err != nil { + return nil, err + } + + return ts, nil +} + func newAppInGuestSpace(appId uint64, guestSpaceId uint64) *App { return &App{ Domain: os.Getenv("KINTONE_DOMAIN"), From 441e96cb538242aa8d65b272113b2e1367b6ab36 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:24:17 +0700 Subject: [PATCH 03/39] create local test server --- app_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app_test.go b/app_test.go index 7fc2446..d951cc7 100644 --- a/app_test.go +++ b/app_test.go @@ -55,6 +55,7 @@ func createResponseLocalTestServer(data string) (*httptest.Server, error) { return ts, nil } + ts.Listener = l func newAppInGuestSpace(appId uint64, guestSpaceId uint64) *App { return &App{ Domain: os.Getenv("KINTONE_DOMAIN"), From 9672c2f7fc9f9f5d4f0219489f49a9e42c105b3c Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:25:30 +0700 Subject: [PATCH 04/39] create local test server --- app_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app_test.go b/app_test.go index d951cc7..8c36620 100644 --- a/app_test.go +++ b/app_test.go @@ -55,7 +55,22 @@ func createResponseLocalTestServer(data string) (*httptest.Server, error) { return ts, nil } +func NewLocalHTTPSTestServer(handler http.Handler) (*httptest.Server, error) { + ts := httptest.NewUnstartedServer(handler) + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + + l, err := net.Listen("tcp", "localhost:8088") + if err != nil { + fmt.Println(err) + } + + ts.Listener.Close() ts.Listener = l + ts.StartTLS() + + return ts, nil +} + func newAppInGuestSpace(appId uint64, guestSpaceId uint64) *App { return &App{ Domain: os.Getenv("KINTONE_DOMAIN"), From 6911f69dca8f653deeaa439b6ef481252e86b9b8 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:26:28 +0700 Subject: [PATCH 05/39] create func TestGetRecordComments --- app_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/app_test.go b/app_test.go index 8c36620..b78dc7c 100644 --- a/app_test.go +++ b/app_test.go @@ -252,14 +252,62 @@ func TestGuestSpace(t *testing.T) { } func TestGetRecordComments(t *testing.T) { - a := newApp(13) - var offset uint64 = 5 + json := ` + { + "comments": [ + { + "id": "3", + "text": "user14 Thank you! Looks great.", + "createdAt": "2016-05-09T18:29:05Z", + "creator": { + "code": "user13", + "name": "user13" + }, + "mentions": [ + { + "code": "user14", + "type": "USER" + } + ] + }, + { + "id": "2", + "text": "user13 Global Sales APAC Taskforce \nHere is today's report.", + "createdAt": "2016-05-09T18:27:54Z", + "creator": { + "code": "user14", + "name": "user14" + }, + "mentions": [ + { + "code": "user13", + "type": "USER" + }, + { + "code": "Global Sales_1BNZeQ", + "type": "ORGANIZATION" + }, + { + "code": "APAC Taskforce_DJrvzu", + "type": "GROUP" + } + ] + } + ], + "older": false, + "newer": false + } + ` + ts, _ := createResponseLocalTestServer(json) + defer ts.Close() + a := newAppForTest("127.0.0.1:8088", "test", "test", 2) + var offset uint64 = 0 var limit uint64 = 10 - if rec, err := a.GetRecordComments(3, "asc", offset, limit); err != nil { + if rec, err := a.GetRecordComments(1, "asc", offset, limit); err != nil { t.Error(err) } else { - if !strings.Contains(rec[0].Id, "6") { - t.Errorf("the first comment id mismatch. expected is 6 but actual %v", rec[0].Id) + if !strings.Contains(rec[0].Id, "3") { + t.Errorf("the first comment id mismatch. expected is 3 but actual %v", rec[0].Id) } } } From 1f88ec20177cffaf165ffc2a55113387de38dfb5 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:27:00 +0700 Subject: [PATCH 06/39] update test get record --- app_test.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app_test.go b/app_test.go index b78dc7c..184161d 100644 --- a/app_test.go +++ b/app_test.go @@ -82,15 +82,31 @@ func newAppInGuestSpace(appId uint64, guestSpaceId uint64) *App { } func TestGetRecord(t *testing.T) { - a := newApp(4799) - if len(a.Password) == 0 { - t.Skip() - } - if rec, err := a.GetRecord(116); err != nil { + json := `{ + "record": { + "Updated_by": { + "type": "MODIFIER", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "$id": { + "type": "__ID__", + "value": "1" + } + } +} +` + ts, _ := createResponseLocalTestServer(json) + defer ts.Close() + a := newAppForTest("127.0.0.1:8088", "test", "test", 2) + + if rec, err := a.GetRecord(1); err != nil { t.Error(err) } else { - if rec.Id() != 116 { + if rec.Id() != 1 { t.Errorf("Unexpected Id: %d", rec.Id()) } for _, f := range rec.Fields { From 45d93582df4d45145e488011c995dea5d6afa6f8 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:42:22 +0700 Subject: [PATCH 07/39] update newAppForTest --- app_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app_test.go b/app_test.go index 184161d..9a4c0c4 100644 --- a/app_test.go +++ b/app_test.go @@ -18,12 +18,12 @@ import ( "time" ) -func newAppForTest(domain string, user string, password string, appID uint64) *App { +func newAppForTest() *App { return &App{ - Domain: domain, - User: user, - Password: password, - AppId: appID, + Domain: "127.0.0.1:8088", + User: "test", + Password: "test", + AppId: 1, } } From b10ab6b8a197998365b458895606012d781e42e1 Mon Sep 17 00:00:00 2001 From: huong Date: Thu, 2 Jan 2020 16:42:52 +0700 Subject: [PATCH 08/39] change parameter newAppForTest --- app_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app_test.go b/app_test.go index 9a4c0c4..6acc2d5 100644 --- a/app_test.go +++ b/app_test.go @@ -101,7 +101,7 @@ func TestGetRecord(t *testing.T) { ` ts, _ := createResponseLocalTestServer(json) defer ts.Close() - a := newAppForTest("127.0.0.1:8088", "test", "test", 2) + a := newAppForTest() if rec, err := a.GetRecord(1); err != nil { t.Error(err) @@ -138,6 +138,7 @@ func TestGetRecord(t *testing.T) { } else { t.Log(len(recs)) } + } func TestAddRecord(t *testing.T) { @@ -316,7 +317,7 @@ func TestGetRecordComments(t *testing.T) { ` ts, _ := createResponseLocalTestServer(json) defer ts.Close() - a := newAppForTest("127.0.0.1:8088", "test", "test", 2) + a := newAppForTest() var offset uint64 = 0 var limit uint64 = 10 if rec, err := a.GetRecordComments(1, "asc", offset, limit); err != nil { From 4d3990e201d70de63bee2713ddf6cfb6ff9d0727 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 15:29:40 +0700 Subject: [PATCH 09/39] create multiple serve test --- app_test.go | 194 ++++++++++++++++++++++------------------------------ 1 file changed, 83 insertions(+), 111 deletions(-) diff --git a/app_test.go b/app_test.go index 7468413..b1d0c3c 100644 --- a/app_test.go +++ b/app_test.go @@ -5,9 +5,9 @@ package kintone import ( - "bytes" "crypto/tls" "fmt" + "io" "io/ioutil" "net" "net/http" @@ -17,92 +17,106 @@ import ( "time" ) -func newAppForTest() *App { - return &App{ - Domain: "127.0.0.1:8088", - User: "test", - Password: "test", - AppId: 1, - } -} - -func newApp(appID uint64) *App { - return &App{ - Domain: KINTONE_DOMAIN, - User: KINTONE_USER, - Password: KINTONE_PASSWORD, - AppId: appID, - } -} +const ( + KINTONE_DOMAIN = "localhost:8088" + KINTONE_USERNAME = "test" + KINTONE_PASSWORD = "test" + KINTONE_APP_ID = 1 + KINTONE_API_TOKEN = "1e42da75-8432-4adb-9a2b-dbb6e7cb3c6b" + KINTONE_GUEST_SPACE_ID = 0 +) -func newAppWithApiToken(appId uint64) *App { - return &App{ - Domain: KINTONE_DOMAIN, - ApiToken: KINTONE_API_TOKEN, - AppId: appId, - } -} +func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { + ts := httptest.NewUnstartedServer(mux) + listen, err := net.Listen("tcp", KINTONE_DOMAIN) -func createResponseLocalTestServer(data string) (*httptest.Server, error) { - ts, err := NewLocalHTTPSTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, data) - })) if err != nil { - return nil, err + fmt.Println(err) } + ts.Listener.Close() + ts.Listener = listen + ts.StartTLS() + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} return ts, nil } -func NewLocalHTTPSTestServer(handler http.Handler) (*httptest.Server, error) { - ts := httptest.NewUnstartedServer(handler) - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} +func createServerMux() (*http.ServeMux, error) { + mux := http.NewServeMux() + mux.HandleFunc("/k/v1/record.json", handleResponseGetRecord) + return mux, nil +} - l, err := net.Listen("tcp", "localhost:8088") +// handler +func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { + testData := GetTestDataGetRecord() + fmt.Fprint(response, testData.output) +} + +func TestMain(m *testing.M) { + mux, err := createServerMux() if err != nil { - fmt.Println(err) + fmt.Println("StartServerTest", err) } - - ts.Listener.Close() - ts.Listener = l - ts.StartTLS() - - return ts, nil + ts, err := createServerTest(mux) + if err != nil { + fmt.Println("createServerTest", err) + } + m.Run() + ts.Close() } -func newAppInGuestSpace(appId uint64, guestSpaceId uint64) *App { +func newApp() *App { return &App{ Domain: KINTONE_DOMAIN, - User: KINTONE_USER, + User: KINTONE_USERNAME, Password: KINTONE_PASSWORD, - AppId: appId, - GuestSpaceId: guestSpaceId, + AppId: KINTONE_APP_ID, + ApiToken: KINTONE_API_TOKEN, + GuestSpaceId: KINTONE_GUEST_SPACE_ID, } } -func TestGetRecord(t *testing.T) { +func TestAddRecord(t *testing.T) { + testData := GetDataTestAddRecord() + a := newApp() - json := `{ - "record": { - "Updated_by": { - "type": "MODIFIER", - "value": { - "code": "Administrator", - "name": "Administrator" - } + fileKey, err := a.Upload(testData.input[0].(string), "text/html", + testData.input[1].(io.Reader)) + if err != nil { + t.Error("Upload failed", err) + } + + rec := NewRecord(map[string]interface{}{ + "title": SingleLineTextField("test!"), + "file": FileField{ + {FileKey: fileKey}, }, - "$id": { - "type": "__ID__", - "value": "1" - } - } -} -` - ts, _ := createResponseLocalTestServer(json) - defer ts.Close() - a := newAppForTest() + }) + _, err = a.AddRecord(rec) + if err != nil { + t.Error("AddRecord failed", rec) + } - if rec, err := a.GetRecord(1); err != nil { + recs := []*Record{ + NewRecord(map[string]interface{}{ + "title": SingleLineTextField("multi add 1"), + }), + NewRecord(map[string]interface{}{ + "title": SingleLineTextField("multi add 2"), + }), + } + ids, err := a.AddRecords(recs) + if err != nil { + t.Error("AddRecords failed", recs) + } else { + t.Log(ids) + } +} +func TestGetRecord(t *testing.T) { + testData := GetTestDataGetRecord() + a := newApp() + if rec, err := a.GetRecord(uint64(testData.input[0].(int))); err != nil { t.Error(err) } else { if rec.Id() != 1 { @@ -139,57 +153,15 @@ func TestGetRecord(t *testing.T) { } } -func TestAddRecord(t *testing.T) { - a := newApp(9004) - if len(a.Password) == 0 { - t.Skip() - } - - fileKey, err := a.Upload("ほげ春巻.txta", "text/html", - bytes.NewReader([]byte(`abc -hoge -`))) - if err != nil { - t.Error("Upload failed", err) - } - - rec := NewRecord(map[string]interface{}{ - "title": SingleLineTextField("test!"), - "file": FileField{ - {FileKey: fileKey}, - }, - }) - _, err = a.AddRecord(rec) - if err != nil { - t.Error("AddRecord failed", rec) - } - - recs := []*Record{ - NewRecord(map[string]interface{}{ - "title": SingleLineTextField("multi add 1"), - }), - NewRecord(map[string]interface{}{ - "title": SingleLineTextField("multi add 2"), - }), - } - ids, err := a.AddRecords(recs) - if err != nil { - t.Error("AddRecords failed", recs) - } else { - t.Log(ids) - } -} - func TestUpdateRecord(t *testing.T) { - a := newApp(9004) - if len(a.Password) == 0 { - t.Skip() - } + testData := GetTestDataGetRecord() + a := newApp() - rec, err := a.GetRecord(4) + rec, err := a.GetRecord(uint64(testData.input[0].(int))) if err != nil { t.Fatal(err) } + rec.Fields["title"] = SingleLineTextField("new title") if err := a.UpdateRecord(rec, true); err != nil { t.Error("UpdateRecord failed", err) From ab6ec7d75d24ed10e1088ce4bcb630f2f2270a10 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:02:22 +0700 Subject: [PATCH 10/39] add handler --- app_test.go | 5 + app_test_json.go | 256 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 app_test_json.go diff --git a/app_test.go b/app_test.go index b1d0c3c..333dc69 100644 --- a/app_test.go +++ b/app_test.go @@ -44,6 +44,11 @@ func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { func createServerMux() (*http.ServeMux, error) { mux := http.NewServeMux() mux.HandleFunc("/k/v1/record.json", handleResponseGetRecord) + mux.HandleFunc("/k/v1/records.json", handleResponseGetRecords) + mux.HandleFunc("/k/v1/record/comments.json", handleResponseGetRecordsComments) + mux.HandleFunc("/k/v1/file.json", handleResponseUploadFile) + mux.HandleFunc("/k/v1/record/comment.json", handleResponseAddRecordComments) + mux.HandleFunc("/k/v1/records/cursor.json", handleResponseRecordsCursor) return mux, nil } diff --git a/app_test_json.go b/app_test_json.go new file mode 100644 index 0000000..6f22b56 --- /dev/null +++ b/app_test_json.go @@ -0,0 +1,256 @@ +package kintone + +import ( + "bytes" +) + +type TestData struct { + input []interface{} + output string +} + +func GetTestDataDeleteRecords() *TestData { + return &TestData{ + output: `{}`, + } +} +func GetTestDataGetRecord() *TestData { + return &TestData{ + input: []interface{}{1}, + output: `{ + "record": { + "Updated_by": { + "type": "MODIFIER", + "value": { + "code": "Administrator", + "name": "Administrator" + }, + "key": "hehehe" + + + }, + + "$id": { + "type": "__ID__", + "value": "1" + } + } + }`, + } +} + +func GetTestDataGetRecords() *TestData { + return &TestData{ + output: `{ + "records": [ + { + "Created_datetime": { + "type": "CREATED_TIME", + "value": "2019-03-11T04:50:00Z" + }, + "Created_by": { + "type": "CREATOR", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "$id": { + "type": "__ID__", + "value": "1" + } + }, + { + "Created_datetime": { + "type": "CREATED_TIME", + "value": "2019-03-11T06:42:00Z" + }, + "Created_by": { + "type": "CREATOR", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "$id": { + "type": "__ID__", + "value": "2" + } + } + ], + "totalCount": null + }`, + } +} + +func GetDataTestUploadFile() *TestData { + return &TestData{ + output: `{ + "app": 3, + "id": 6, + "record": { + "attached_file": { + "value": [ + { + "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" + } + ] + } + } + }`, + } +} + +func GetDataTestRecordComments() *TestData { + return &TestData{ + output: `{ + "comments": [ + { + "id": "3", + "text": "user14 Thank you! Looks great.", + "createdAt": "2016-05-09T18:29:05Z", + "creator": { + "code": "user13", + "name": "user13" + }, + "mentions": [ + { + "code": "user14", + "type": "USER" + } + ] + }, + { + "id": "2", + "text": "user13 Global Sales APAC Taskforce \nHere is today's report.", + "createdAt": "2016-05-09T18:27:54Z", + "creator": { + "code": "user14", + "name": "user14" + }, + "mentions": [ + { + "code": "user13", + "type": "USER" + }, + { + "code": "Global Sales_1BNZeQ", + "type": "ORGANIZATION" + }, + { + "code": "APAC Taskforce_DJrvzu", + "type": "GROUP" + } + ] + } + ], + "older": false, + "newer": false + }`, + } +} + +func GetDataTestAddRecord() *TestData { + return &TestData{ + input: []interface{}{ + "ほげ春巻.txta", + bytes.NewReader([]byte(`abc + hoge + `)), + }, + output: `{ + "id": "1", + "revision": "1" + }`, + } +} +func getDataTestCreateCursor() *TestData { + return &TestData{ + output: `{ + "id": "9a9716fe-1394-4677-a1c7-2199a5d28215", + "totalCount": 123456 + }`, + } + +} +func GetDataTestGetRecordsByCursor() *TestData { + + return &TestData{ + input: []interface{}{"9a9716fe-1394-4677-a1c7-2199a5d28215"}, + output: `{ + "records": [ + { + "$id": { + "type": "__ID__", + "value": "1" + }, + "Created_by": { + "type": "CREATOR", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "Created_datetime": { + "type": "CREATED_TIME", + "value": "2019-05-23T04:50:00Z" + } + } + ], + "next": false + }`, + } +} + +func GetTestDataDeleteCursor() *TestData { + return &TestData{ + input: []interface{}{"9a9716fe-1394-4677-a1c7-2199a5d28215"}, + output: `{}`, + } +} + +func GetTestDataCreateCursor() *TestData { + return &TestData{ + input: []interface{}{[]string{"$id", "date"}, "", 100}, + output: `{"id":"9a9716fe-1394-4677-a1c7-2199a5d28215"}`, + } +} + +func GetTestDataAddRecordComment() *TestData { + return &TestData{ + input: []interface{}{2}, + output: `{"id": "4"}`, + } +} +func GetTestDataUpdateRecordByKey() *TestData { + return &TestData{ + input: []interface{}{2}, + output: `{ + "app": 1, + "records": [ + { + "updateKey": { + "field": "unique_key", + "value": "CODE123" + }, + "record": { + "Text": { + "value": "Silver plates" + } + } + }, + { + "updateKey": { + "field": "unique_key", + "value": "CODE456" + }, + "record": { + "Text": { + "value": "The quick brown fox." + } + } + } + ] + }`, + } +} From 061b97c0bf8eab1f2d5f2f3faf10ef43a3e8fcc1 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:05:30 +0700 Subject: [PATCH 11/39] create handleResponseRecordsCursor, handleResponseAddRecordComments, handleResponseUploadFile, handleResponseGetRecord, handleResponseGetRecords, handleResponseGetRecordsComments --- app_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/app_test.go b/app_test.go index 333dc69..65e5509 100644 --- a/app_test.go +++ b/app_test.go @@ -31,7 +31,7 @@ func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { listen, err := net.Listen("tcp", KINTONE_DOMAIN) if err != nil { - fmt.Println(err) + return nil, err } ts.Listener.Close() @@ -52,10 +52,59 @@ func createServerMux() (*http.ServeMux, error) { return mux, nil } -// handler +// handler mux +func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + testData := GetDataTestGetRecordsByCursor() + fmt.Fprint(response, testData.output) + } else if r.Method == "DELETE" { + testData := GetTestDataDeleteCursor() + fmt.Fprint(response, testData.output) + } else if r.Method == "POST" { + testData := GetTestDataCreateCursor() + fmt.Fprint(response, testData.output) + } +} +func handleResponseAddRecordComments(response http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + testData := GetTestDataAddRecordComment() + fmt.Fprint(response, testData.output) + } +} + +func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + testData := GetDataTestUploadFile() + fmt.Fprint(response, testData.output) + } +} + func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { - testData := GetTestDataGetRecord() + if r.Method == "GET" { + testData := GetTestDataGetRecord() + fmt.Fprint(response, testData.output) + } else if r.Method == "PUT" { + testData := GetTestDataUpdateRecordByKey() + fmt.Fprint(response, testData.output) + } + +} + +func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + testData := GetTestDataGetRecords() + fmt.Fprint(response, testData.output) + } else if r.Method == "DELETE" { + testData := GetTestDataDeleteRecords() + fmt.Fprint(response, testData.output) + } + +} + +func handleResponseGetRecordsComments(response http.ResponseWriter, r *http.Request) { + testData := GetDataTestRecordComments() fmt.Fprint(response, testData.output) + } func TestMain(m *testing.M) { @@ -166,22 +215,30 @@ func TestUpdateRecord(t *testing.T) { if err != nil { t.Fatal(err) } - rec.Fields["title"] = SingleLineTextField("new title") if err := a.UpdateRecord(rec, true); err != nil { t.Error("UpdateRecord failed", err) } + rec.Fields["key"] = SingleLineTextField(` { + "field": "unique_key", + "value": "unique_code" + }`) if err := a.UpdateRecordByKey(rec, true, "key"); err != nil { + t.Error("UpdateRecordByKey failed", err) } - recs, err := a.GetRecords(nil, "limit 3") if err != nil { t.Fatal(err) } + for _, rec := range recs { rec.Fields["title"] = SingleLineTextField(time.Now().String()) + rec.Fields["key"] = SingleLineTextField(` { + "field": "unique_key", + "value": "unique_code" + }`) } if err := a.UpdateRecords(recs, true); err != nil { t.Error("UpdateRecords failed", err) From f417e5a8ad68fe5eb25d897b9be2182c16f963b1 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:47:42 +0700 Subject: [PATCH 12/39] defined app test --- app_test.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app_test.go b/app_test.go index 65e5509..123ac35 100644 --- a/app_test.go +++ b/app_test.go @@ -23,7 +23,7 @@ const ( KINTONE_PASSWORD = "test" KINTONE_APP_ID = 1 KINTONE_API_TOKEN = "1e42da75-8432-4adb-9a2b-dbb6e7cb3c6b" - KINTONE_GUEST_SPACE_ID = 0 + KINTONE_GUEST_SPACE_ID = 1 ) func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { @@ -121,15 +121,28 @@ func TestMain(m *testing.M) { } func newApp() *App { + return &App{ + Domain: KINTONE_DOMAIN, + User: KINTONE_USERNAME, + Password: KINTONE_PASSWORD, + AppId: KINTONE_APP_ID, + ApiToken: KINTONE_API_TOKEN, + } +} +func newAppWithGuest() *App { return &App{ Domain: KINTONE_DOMAIN, - User: KINTONE_USERNAME, - Password: KINTONE_PASSWORD, AppId: KINTONE_APP_ID, ApiToken: KINTONE_API_TOKEN, GuestSpaceId: KINTONE_GUEST_SPACE_ID, } } +func newAppWithToken() *App { + return &App{ + Domain: KINTONE_DOMAIN, + ApiToken: KINTONE_API_TOKEN, + } +} func TestAddRecord(t *testing.T) { testData := GetDataTestAddRecord() From bb120f1de87f218b27cc2671b5c178fc64ee29f0 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:48:23 +0700 Subject: [PATCH 13/39] defined handler test form , add record comment --- app_test.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app_test.go b/app_test.go index 123ac35..8e0e7db 100644 --- a/app_test.go +++ b/app_test.go @@ -47,12 +47,21 @@ func createServerMux() (*http.ServeMux, error) { mux.HandleFunc("/k/v1/records.json", handleResponseGetRecords) mux.HandleFunc("/k/v1/record/comments.json", handleResponseGetRecordsComments) mux.HandleFunc("/k/v1/file.json", handleResponseUploadFile) - mux.HandleFunc("/k/v1/record/comment.json", handleResponseAddRecordComments) + mux.HandleFunc("/k/v1/record/comment.json", handleResponseRecordComments) mux.HandleFunc("/k/v1/records/cursor.json", handleResponseRecordsCursor) + mux.HandleFunc("/k/v1/form.json", handleResponseForm) + mux.HandleFunc("/k/guest/1/v1/form.json", handleResponseForm) return mux, nil } // handler mux +func handleResponseForm(response http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + testData := GetDataTestForm() + fmt.Fprint(response, testData.output) + } +} + func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) { if r.Method == "GET" { testData := GetDataTestGetRecordsByCursor() @@ -65,10 +74,13 @@ func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) fmt.Fprint(response, testData.output) } } -func handleResponseAddRecordComments(response http.ResponseWriter, r *http.Request) { +func handleResponseRecordComments(response http.ResponseWriter, r *http.Request) { if r.Method == "POST" { testData := GetTestDataAddRecordComment() fmt.Fprint(response, testData.output) + } else if r.Method == "DELETE" { + testData := GetDataTestDeleteRecordComment() + fmt.Fprint(response, testData.output) } } From 40aa7c0643fff6db74c8f80369263e9112ccf855 Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:49:29 +0700 Subject: [PATCH 14/39] implement function test TestAddRecordComment, TestGetRecordComments, TestGuestSpace, TestApiToken, TestFields, TestCreateCursor, TestDeleteCursor, TestGetRecordsByCursor --- app_test.go | 128 ++++++++++------------------------------------------ 1 file changed, 25 insertions(+), 103 deletions(-) diff --git a/app_test.go b/app_test.go index 8e0e7db..ab2a21f 100644 --- a/app_test.go +++ b/app_test.go @@ -98,6 +98,9 @@ func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { } else if r.Method == "PUT" { testData := GetTestDataUpdateRecordByKey() fmt.Fprint(response, testData.output) + } else if r.Method == "POST" { + testData := GetTestDataAddRecord() + fmt.Fprint(response, testData.output) } } @@ -109,6 +112,9 @@ func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { } else if r.Method == "DELETE" { testData := GetTestDataDeleteRecords() fmt.Fprint(response, testData.output) + } else if r.Method == "POST" { + testData := GetTestDataAddRecords() + fmt.Fprint(response, testData.output) } } @@ -176,7 +182,6 @@ func TestAddRecord(t *testing.T) { if err != nil { t.Error("AddRecord failed", rec) } - recs := []*Record{ NewRecord(map[string]interface{}{ "title": SingleLineTextField("multi add 1"), @@ -275,10 +280,7 @@ func TestUpdateRecord(t *testing.T) { } func TestDeleteRecord(t *testing.T) { - a := newApp(9004) - if len(a.Password) == 0 { - t.Skip() - } + a := newApp() ids := []uint64{6, 7} if err := a.DeleteRecords(ids); err != nil { @@ -287,62 +289,35 @@ func TestDeleteRecord(t *testing.T) { } func TestGetRecordsByCursor(t *testing.T) { - app := newApp(18) - - if len(app.Password) == 0 { - t.Skip() - } - - cursor := app.createCursorForTest() - record, err := app.GetRecordsByCursor(string(cursor.Id)) - + testData := GetDataTestGetRecordsByCursor() + app := newApp() + _, err := app.GetRecordsByCursor(testData.input[0].(string)) if err != nil { t.Errorf("TestGetCursor is failed: %v", err) } - fmt.Println(record) - -} -func (app *App) createCursorForTest() *Cursor { - cursor, err := app.CreateCursor([]string{"$id", "Status"}, "", 400) - fmt.Println("cursor", cursor) - if err != nil { - fmt.Println("createCursorForTest failed: ", err) - } - return cursor } func TestDeleteCursor(t *testing.T) { - app := newApp(18) - if len(app.Password) == 0 { - t.Skip() - } - - cursor := app.createCursorForTest() - fmt.Println("cursor", cursor) - err := app.DeleteCursor(string(cursor.Id)) - + testData := GetTestDataDeleteCursor() + app := newApp() + err := app.DeleteCursor(testData.input[0].(string)) if err != nil { t.Errorf("TestDeleteCursor is failed: %v", err) } } func TestCreateCursor(t *testing.T) { - app := newApp(18) - if len(app.Password) == 0 { - t.Skip() - } - _, err := app.CreateCursor([]string{"$id", "date"}, "", 100) + testData := GetTestDataCreateCursor() + app := newApp() + _, err := app.CreateCursor(testData.input[0].([]string), testData.input[1].(string), uint64(testData.input[2].(int))) if err != nil { t.Errorf("TestCreateCurSor is failed: %v", err) } } func TestFields(t *testing.T) { - a := newApp(8326) - if len(a.Password) == 0 { - t.Skip() - } + a := newApp() fi, err := a.Fields() if err != nil { @@ -354,11 +329,7 @@ func TestFields(t *testing.T) { } func TestApiToken(t *testing.T) { - a := newAppWithApiToken(9974) - if len(a.ApiToken) == 0 { - t.Skip() - } - + a := newAppWithToken() _, err := a.Fields() if err != nil { t.Error("Api token failed", err) @@ -366,10 +337,7 @@ func TestApiToken(t *testing.T) { } func TestGuestSpace(t *testing.T) { - a := newAppInGuestSpace(185, 9) - if len(a.Password) == 0 { - t.Skip() - } + a := newAppWithGuest() _, err := a.Fields() if err != nil { @@ -378,55 +346,7 @@ func TestGuestSpace(t *testing.T) { } func TestGetRecordComments(t *testing.T) { - json := ` - { - "comments": [ - { - "id": "3", - "text": "user14 Thank you! Looks great.", - "createdAt": "2016-05-09T18:29:05Z", - "creator": { - "code": "user13", - "name": "user13" - }, - "mentions": [ - { - "code": "user14", - "type": "USER" - } - ] - }, - { - "id": "2", - "text": "user13 Global Sales APAC Taskforce \nHere is today's report.", - "createdAt": "2016-05-09T18:27:54Z", - "creator": { - "code": "user14", - "name": "user14" - }, - "mentions": [ - { - "code": "user13", - "type": "USER" - }, - { - "code": "Global Sales_1BNZeQ", - "type": "ORGANIZATION" - }, - { - "code": "APAC Taskforce_DJrvzu", - "type": "GROUP" - } - ] - } - ], - "older": false, - "newer": false - } - ` - ts, _ := createResponseLocalTestServer(json) - defer ts.Close() - a := newAppForTest() + a := newApp() var offset uint64 = 0 var limit uint64 = 10 if rec, err := a.GetRecordComments(1, "asc", offset, limit); err != nil { @@ -437,15 +357,17 @@ func TestGetRecordComments(t *testing.T) { } } } + func TestAddRecordComment(t *testing.T) { - appTest := newApp(12) + testData := GetTestDataAddRecordComment() + appTest := newApp() mentionMemberCybozu := &ObjMention{Code: "cybozu", Type: ConstCommentMentionTypeUser} mentionGroupAdmin := &ObjMention{Code: "Administrators", Type: ConstCommentMentionTypeGroup} mentionDepartmentAdmin := &ObjMention{Code: "Admin", Type: ConstCommentMentionTypeDepartment} var cmt Comment cmt.Text = "Test comment 222" cmt.Mentions = []*ObjMention{mentionGroupAdmin, mentionMemberCybozu, mentionDepartmentAdmin} - cmtID, err := appTest.AddRecordComment(2, &cmt) + cmtID, err := appTest.AddRecordComment(uint64(testData.input[0].(int)), &cmt) if err != nil { t.Error(err) @@ -455,7 +377,7 @@ func TestAddRecordComment(t *testing.T) { } func TestDeleteComment(t *testing.T) { - appTest := newApp(4) + appTest := newApp() var cmtID uint64 = 14 err := appTest.DeleteComment(3, 12) From 0f7e370ef18970e957843a72513dc42484b1a72b Mon Sep 17 00:00:00 2001 From: huong Date: Fri, 3 Jan 2020 17:49:56 +0700 Subject: [PATCH 15/39] add data test json --- app_test_json.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/app_test_json.go b/app_test_json.go index 6f22b56..78bbd20 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -150,6 +150,81 @@ func GetDataTestRecordComments() *TestData { } } +func GetDataTestForm() *TestData { + return &TestData{ + output: `{ + "properties": [ + { + "code": "string_1", + "defaultValue": "", + "expression": "", + "hideExpression": "false", + "maxLength": "64", + "minLength": null, + "label": "string_1", + "noLabel": "false", + "required": "true", + "type": "SINGLE_LINE_TEXT", + "unique": "true" + }, + { + "code": "number_1", + "defaultValue": "12345", + "digit": "true", + "displayScale": "4", + "expression": "", + "maxValue": null, + "minValue": null, + "label": "number_1", + "noLabel": "true", + "required": "false", + "type": "NUMBER", + "unique": "false" + }, + { + "code": "checkbox_1", + "defaultValue": [ + "sample1", + "sample3" + ], + "label": "checkbox_1", + "noLabel": "false", + "options": [ + "sample1", + "sample2", + "sample3" + ], + "required": "false", + "type": "CHECK_BOX" + } + ] + }`, + } +} + +func GetDataTestDeleteRecordComment() *TestData { + return &TestData{ + output: `{}`, + } +} +func GetTestDataAddRecord() *TestData { + return &TestData{ + output: `{ + "id": "1", + "revision": "1" + }`, + } +} + +func GetTestDataAddRecords() *TestData { + return &TestData{ + output: `{ + "ids": ["77","78"], + "revisions": ["1","1"] + }`, + } +} + func GetDataTestAddRecord() *TestData { return &TestData{ input: []interface{}{ From ef36d729919f630e972f7e4aeb24b8bcf36f9ccd Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 11:10:34 +0700 Subject: [PATCH 16/39] add check header --- app_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app_test.go b/app_test.go index ab2a21f..5d4ac53 100644 --- a/app_test.go +++ b/app_test.go @@ -12,6 +12,7 @@ import ( "net" "net/http" "net/http/httptest" + "os" "strings" "testing" "time" @@ -24,6 +25,8 @@ const ( KINTONE_APP_ID = 1 KINTONE_API_TOKEN = "1e42da75-8432-4adb-9a2b-dbb6e7cb3c6b" KINTONE_GUEST_SPACE_ID = 1 + AUTH_HEADER_TOKEN = "X-Cybozu-API-Token" + AUTH_HEADER_PASSWORD = "X-Cybozu-Authorization" ) func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { @@ -54,8 +57,23 @@ func createServerMux() (*http.ServeMux, error) { return mux, nil } +// header check +func checkHeader(response http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") + authPassword := r.Header.Get(AUTH_HEADER_PASSWORD) + authToken := r.Header.Get(AUTH_HEADER_TOKEN) + if contentType != "application/json" { + fmt.Println("Content-type is invalid") + os.Exit(1) + } else if authPassword == "" && authToken == "" { + fmt.Println("Authenticated is fail") + os.Exit(1) + } +} + // handler mux func handleResponseForm(response http.ResponseWriter, r *http.Request) { + checkHeader(response, r) if r.Method == "GET" { testData := GetDataTestForm() fmt.Fprint(response, testData.output) @@ -67,14 +85,17 @@ func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) testData := GetDataTestGetRecordsByCursor() fmt.Fprint(response, testData.output) } else if r.Method == "DELETE" { + checkHeader(response, r) testData := GetTestDataDeleteCursor() fmt.Fprint(response, testData.output) } else if r.Method == "POST" { + checkHeader(response, r) testData := GetTestDataCreateCursor() fmt.Fprint(response, testData.output) } } func handleResponseRecordComments(response http.ResponseWriter, r *http.Request) { + checkHeader(response, r) if r.Method == "POST" { testData := GetTestDataAddRecordComment() fmt.Fprint(response, testData.output) @@ -85,6 +106,7 @@ func handleResponseRecordComments(response http.ResponseWriter, r *http.Request) } func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { + // checkHeader(response, r) if r.Method == "POST" { testData := GetDataTestUploadFile() fmt.Fprint(response, testData.output) @@ -92,6 +114,7 @@ func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { + checkHeader(response, r) if r.Method == "GET" { testData := GetTestDataGetRecord() fmt.Fprint(response, testData.output) @@ -106,6 +129,7 @@ func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { + checkHeader(response, r) if r.Method == "GET" { testData := GetTestDataGetRecords() fmt.Fprint(response, testData.output) @@ -120,6 +144,7 @@ func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecordsComments(response http.ResponseWriter, r *http.Request) { + checkHeader(response, r) testData := GetDataTestRecordComments() fmt.Fprint(response, testData.output) From fc42a6a734000052c578feca3188b8455782b320 Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 11:37:17 +0700 Subject: [PATCH 17/39] spli function check content type and check auth --- app_test.go | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/app_test.go b/app_test.go index 5d4ac53..b06b197 100644 --- a/app_test.go +++ b/app_test.go @@ -12,7 +12,6 @@ import ( "net" "net/http" "net/http/httptest" - "os" "strings" "testing" "time" @@ -58,55 +57,65 @@ func createServerMux() (*http.ServeMux, error) { } // header check -func checkHeader(response http.ResponseWriter, r *http.Request) { - contentType := r.Header.Get("Content-Type") +func checkAuth(response http.ResponseWriter, r *http.Request) { authPassword := r.Header.Get(AUTH_HEADER_PASSWORD) authToken := r.Header.Get(AUTH_HEADER_TOKEN) + if authPassword == "" && authToken == "" { + http.Error(response, "Authenticated is fail!", http.StatusForbidden) + + response.WriteHeader(http.StatusInternalServerError) + response.Write([]byte("500 - Authenticated is fail!")) + } +} +func checkContentType(response http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") if contentType != "application/json" { fmt.Println("Content-type is invalid") - os.Exit(1) - } else if authPassword == "" && authToken == "" { - fmt.Println("Authenticated is fail") - os.Exit(1) + http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) + } } // handler mux func handleResponseForm(response http.ResponseWriter, r *http.Request) { - checkHeader(response, r) + checkAuth(response, r) if r.Method == "GET" { + checkContentType(response, r) testData := GetDataTestForm() fmt.Fprint(response, testData.output) } } func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) { + checkAuth(response, r) if r.Method == "GET" { testData := GetDataTestGetRecordsByCursor() fmt.Fprint(response, testData.output) } else if r.Method == "DELETE" { - checkHeader(response, r) + checkContentType(response, r) testData := GetTestDataDeleteCursor() fmt.Fprint(response, testData.output) } else if r.Method == "POST" { - checkHeader(response, r) + checkContentType(response, r) testData := GetTestDataCreateCursor() fmt.Fprint(response, testData.output) } } func handleResponseRecordComments(response http.ResponseWriter, r *http.Request) { - checkHeader(response, r) + checkAuth(response, r) if r.Method == "POST" { + checkContentType(response, r) testData := GetTestDataAddRecordComment() fmt.Fprint(response, testData.output) } else if r.Method == "DELETE" { + checkContentType(response, r) testData := GetDataTestDeleteRecordComment() fmt.Fprint(response, testData.output) } } func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { - // checkHeader(response, r) + checkAuth(response, r) if r.Method == "POST" { testData := GetDataTestUploadFile() fmt.Fprint(response, testData.output) @@ -114,14 +123,17 @@ func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { - checkHeader(response, r) + checkAuth(response, r) if r.Method == "GET" { + checkContentType(response, r) testData := GetTestDataGetRecord() fmt.Fprint(response, testData.output) } else if r.Method == "PUT" { + checkContentType(response, r) testData := GetTestDataUpdateRecordByKey() fmt.Fprint(response, testData.output) } else if r.Method == "POST" { + checkContentType(response, r) testData := GetTestDataAddRecord() fmt.Fprint(response, testData.output) } @@ -129,14 +141,17 @@ func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { - checkHeader(response, r) + checkAuth(response, r) if r.Method == "GET" { + checkContentType(response, r) testData := GetTestDataGetRecords() fmt.Fprint(response, testData.output) } else if r.Method == "DELETE" { + checkContentType(response, r) testData := GetTestDataDeleteRecords() fmt.Fprint(response, testData.output) } else if r.Method == "POST" { + checkContentType(response, r) testData := GetTestDataAddRecords() fmt.Fprint(response, testData.output) } @@ -144,7 +159,8 @@ func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { } func handleResponseGetRecordsComments(response http.ResponseWriter, r *http.Request) { - checkHeader(response, r) + checkAuth(response, r) + checkContentType(response, r) testData := GetDataTestRecordComments() fmt.Fprint(response, testData.output) From 63b5f34340cf5fd622c1fdbe6eb4edd6b045ab7d Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 11:39:00 +0700 Subject: [PATCH 18/39] change throw error check response --- app_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app_test.go b/app_test.go index b06b197..7be7e3d 100644 --- a/app_test.go +++ b/app_test.go @@ -24,7 +24,7 @@ const ( KINTONE_APP_ID = 1 KINTONE_API_TOKEN = "1e42da75-8432-4adb-9a2b-dbb6e7cb3c6b" KINTONE_GUEST_SPACE_ID = 1 - AUTH_HEADER_TOKEN = "X-Cybozu-API-Token" + AUTH_HEADER_TOKEN = "X-Cybozu1-API-Token" AUTH_HEADER_PASSWORD = "X-Cybozu-Authorization" ) @@ -61,16 +61,12 @@ func checkAuth(response http.ResponseWriter, r *http.Request) { authPassword := r.Header.Get(AUTH_HEADER_PASSWORD) authToken := r.Header.Get(AUTH_HEADER_TOKEN) if authPassword == "" && authToken == "" { - http.Error(response, "Authenticated is fail!", http.StatusForbidden) - - response.WriteHeader(http.StatusInternalServerError) - response.Write([]byte("500 - Authenticated is fail!")) + http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) } } func checkContentType(response http.ResponseWriter, r *http.Request) { contentType := r.Header.Get("Content-Type") if contentType != "application/json" { - fmt.Println("Content-type is invalid") http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) } From 84eef96937f9889f8052807ed9d1867fd1ebd23f Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 11:49:52 +0700 Subject: [PATCH 19/39] const token-valid --- app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_test.go b/app_test.go index 7be7e3d..40e0311 100644 --- a/app_test.go +++ b/app_test.go @@ -24,7 +24,7 @@ const ( KINTONE_APP_ID = 1 KINTONE_API_TOKEN = "1e42da75-8432-4adb-9a2b-dbb6e7cb3c6b" KINTONE_GUEST_SPACE_ID = 1 - AUTH_HEADER_TOKEN = "X-Cybozu1-API-Token" + AUTH_HEADER_TOKEN = "X-Cybozu-API-Token" AUTH_HEADER_PASSWORD = "X-Cybozu-Authorization" ) From 2c82ec765241b94b5d9cb4b4ce3a5fa071ba349f Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 13:49:22 +0700 Subject: [PATCH 20/39] fix review #4 --- app_test.go | 102 +++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/app_test.go b/app_test.go index 40e0311..8494c9a 100644 --- a/app_test.go +++ b/app_test.go @@ -6,6 +6,7 @@ package kintone import ( "crypto/tls" + "encoding/base64" "fmt" "io" "io/ioutil" @@ -57,15 +58,18 @@ func createServerMux() (*http.ServeMux, error) { } // header check -func checkAuth(response http.ResponseWriter, r *http.Request) { - authPassword := r.Header.Get(AUTH_HEADER_PASSWORD) - authToken := r.Header.Get(AUTH_HEADER_TOKEN) - if authPassword == "" && authToken == "" { +func checkAuth(response http.ResponseWriter, request *http.Request) { + authPassword := request.Header.Get(AUTH_HEADER_PASSWORD) + authToken := request.Header.Get(AUTH_HEADER_TOKEN) + userAndPass := base64.StdEncoding.EncodeToString( + []byte(KINTONE_USERNAME + ":" + KINTONE_USERNAME)) + if authPassword != userAndPass && authToken != KINTONE_API_TOKEN { http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) } + } -func checkContentType(response http.ResponseWriter, r *http.Request) { - contentType := r.Header.Get("Content-Type") +func checkContentType(response http.ResponseWriter, request *http.Request) { + contentType := request.Header.Get("Content-Type") if contentType != "application/json" { http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) @@ -73,90 +77,90 @@ func checkContentType(response http.ResponseWriter, r *http.Request) { } // handler mux -func handleResponseForm(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "GET" { - checkContentType(response, r) +func handleResponseForm(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "GET" { + checkContentType(response, request) testData := GetDataTestForm() fmt.Fprint(response, testData.output) } } -func handleResponseRecordsCursor(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "GET" { +func handleResponseRecordsCursor(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "GET" { testData := GetDataTestGetRecordsByCursor() fmt.Fprint(response, testData.output) - } else if r.Method == "DELETE" { - checkContentType(response, r) + } else if request.Method == "DELETE" { + checkContentType(response, request) testData := GetTestDataDeleteCursor() fmt.Fprint(response, testData.output) - } else if r.Method == "POST" { - checkContentType(response, r) + } else if request.Method == "POST" { + checkContentType(response, request) testData := GetTestDataCreateCursor() fmt.Fprint(response, testData.output) } } -func handleResponseRecordComments(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "POST" { - checkContentType(response, r) +func handleResponseRecordComments(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "POST" { + checkContentType(response, request) testData := GetTestDataAddRecordComment() fmt.Fprint(response, testData.output) - } else if r.Method == "DELETE" { - checkContentType(response, r) + } else if request.Method == "DELETE" { + checkContentType(response, request) testData := GetDataTestDeleteRecordComment() fmt.Fprint(response, testData.output) } } -func handleResponseUploadFile(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "POST" { +func handleResponseUploadFile(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "POST" { testData := GetDataTestUploadFile() fmt.Fprint(response, testData.output) } } -func handleResponseGetRecord(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "GET" { - checkContentType(response, r) +func handleResponseGetRecord(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "GET" { + checkContentType(response, request) testData := GetTestDataGetRecord() fmt.Fprint(response, testData.output) - } else if r.Method == "PUT" { - checkContentType(response, r) + } else if request.Method == "PUT" { + checkContentType(response, request) testData := GetTestDataUpdateRecordByKey() fmt.Fprint(response, testData.output) - } else if r.Method == "POST" { - checkContentType(response, r) + } else if request.Method == "POST" { + checkContentType(response, request) testData := GetTestDataAddRecord() fmt.Fprint(response, testData.output) } } -func handleResponseGetRecords(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - if r.Method == "GET" { - checkContentType(response, r) +func handleResponseGetRecords(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + if request.Method == "GET" { + checkContentType(response, request) testData := GetTestDataGetRecords() fmt.Fprint(response, testData.output) - } else if r.Method == "DELETE" { - checkContentType(response, r) + } else if request.Method == "DELETE" { + checkContentType(response, request) testData := GetTestDataDeleteRecords() fmt.Fprint(response, testData.output) - } else if r.Method == "POST" { - checkContentType(response, r) + } else if request.Method == "POST" { + checkContentType(response, request) testData := GetTestDataAddRecords() fmt.Fprint(response, testData.output) } } -func handleResponseGetRecordsComments(response http.ResponseWriter, r *http.Request) { - checkAuth(response, r) - checkContentType(response, r) +func handleResponseGetRecordsComments(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + checkContentType(response, request) testData := GetDataTestRecordComments() fmt.Fprint(response, testData.output) @@ -201,9 +205,9 @@ func newAppWithToken() *App { func TestAddRecord(t *testing.T) { testData := GetDataTestAddRecord() - a := newApp() + app := newApp() - fileKey, err := a.Upload(testData.input[0].(string), "text/html", + fileKey, err := app.Upload(testData.input[0].(string), testData.input[2].(string), testData.input[1].(io.Reader)) if err != nil { t.Error("Upload failed", err) @@ -215,7 +219,7 @@ func TestAddRecord(t *testing.T) { {FileKey: fileKey}, }, }) - _, err = a.AddRecord(rec) + _, err = app.AddRecord(rec) if err != nil { t.Error("AddRecord failed", rec) } @@ -227,7 +231,7 @@ func TestAddRecord(t *testing.T) { "title": SingleLineTextField("multi add 2"), }), } - ids, err := a.AddRecords(recs) + ids, err := app.AddRecords(recs) if err != nil { t.Error("AddRecords failed", recs) } else { From 7e2ec0d8074fde5dc2639e752bac4da35b16972a Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 13:51:06 +0700 Subject: [PATCH 21/39] CHANGE SPACE JSON --- app_test_json.go | 187 ++++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 93 deletions(-) diff --git a/app_test_json.go b/app_test_json.go index 78bbd20..2456e47 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -18,31 +18,31 @@ func GetTestDataGetRecord() *TestData { return &TestData{ input: []interface{}{1}, output: `{ - "record": { - "Updated_by": { - "type": "MODIFIER", - "value": { - "code": "Administrator", - "name": "Administrator" - }, - "key": "hehehe" + "record": { + "Updated_by": { + "type": "MODIFIER", + "value": { + "code": "Administrator", + "name": "Administrator" + }, + "key": "hehehe" - }, + }, - "$id": { - "type": "__ID__", - "value": "1" - } - } - }`, + "$id": { + "type": "__ID__", + "value": "1" + } + } + }`, } } func GetTestDataGetRecords() *TestData { return &TestData{ output: `{ - "records": [ + "records": [ { "Created_datetime": { "type": "CREATED_TIME", @@ -79,32 +79,32 @@ func GetTestDataGetRecords() *TestData { } ], "totalCount": null - }`, + }`, } } func GetDataTestUploadFile() *TestData { return &TestData{ output: `{ - "app": 3, - "id": 6, - "record": { - "attached_file": { - "value": [ - { - "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" - } - ] - } - } - }`, + "app": 3, + "id": 6, + "record": { + "attached_file": { + "value": [ + { + "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" + } + ] + } + } + }`, } } func GetDataTestRecordComments() *TestData { return &TestData{ output: `{ - "comments": [ + "comments": [ { "id": "3", "text": "user14 Thank you! Looks great.", @@ -146,14 +146,14 @@ func GetDataTestRecordComments() *TestData { ], "older": false, "newer": false - }`, + }`, } } func GetDataTestForm() *TestData { return &TestData{ output: `{ - "properties": [ + "properties": [ { "code": "string_1", "defaultValue": "", @@ -198,7 +198,7 @@ func GetDataTestForm() *TestData { "type": "CHECK_BOX" } ] - }`, + }`, } } @@ -210,18 +210,18 @@ func GetDataTestDeleteRecordComment() *TestData { func GetTestDataAddRecord() *TestData { return &TestData{ output: `{ - "id": "1", + "id": "1", "revision": "1" - }`, + }`, } } func GetTestDataAddRecords() *TestData { return &TestData{ output: `{ - "ids": ["77","78"], - "revisions": ["1","1"] - }`, + "ids": ["77","78"], + "revisions": ["1","1"] + }`, } } @@ -230,21 +230,22 @@ func GetDataTestAddRecord() *TestData { input: []interface{}{ "ほげ春巻.txta", bytes.NewReader([]byte(`abc - hoge - `)), + hoge + `)), + "text/html", }, output: `{ - "id": "1", - "revision": "1" - }`, + "id": "1", + "revision": "1" + }`, } } func getDataTestCreateCursor() *TestData { return &TestData{ output: `{ - "id": "9a9716fe-1394-4677-a1c7-2199a5d28215", - "totalCount": 123456 - }`, + "id": "9a9716fe-1394-4677-a1c7-2199a5d28215", + "totalCount": 123456 + }`, } } @@ -253,27 +254,27 @@ func GetDataTestGetRecordsByCursor() *TestData { return &TestData{ input: []interface{}{"9a9716fe-1394-4677-a1c7-2199a5d28215"}, output: `{ - "records": [ - { - "$id": { - "type": "__ID__", - "value": "1" - }, - "Created_by": { - "type": "CREATOR", - "value": { - "code": "Administrator", - "name": "Administrator" - } - }, - "Created_datetime": { - "type": "CREATED_TIME", - "value": "2019-05-23T04:50:00Z" - } - } - ], - "next": false - }`, + "records": [ + { + "$id": { + "type": "__ID__", + "value": "1" + }, + "Created_by": { + "type": "CREATOR", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "Created_datetime": { + "type": "CREATED_TIME", + "value": "2019-05-23T04:50:00Z" + } + } + ], + "next": false + }`, } } @@ -301,31 +302,31 @@ func GetTestDataUpdateRecordByKey() *TestData { return &TestData{ input: []interface{}{2}, output: `{ - "app": 1, - "records": [ - { - "updateKey": { - "field": "unique_key", - "value": "CODE123" - }, - "record": { - "Text": { - "value": "Silver plates" - } - } - }, - { - "updateKey": { - "field": "unique_key", - "value": "CODE456" - }, - "record": { - "Text": { - "value": "The quick brown fox." - } - } - } - ] - }`, + "app": 1, + "records": [ + { + "updateKey": { + "field": "unique_key", + "value": "CODE123" + }, + "record": { + "Text": { + "value": "Silver plates" + } + } + }, + { + "updateKey": { + "field": "unique_key", + "value": "CODE456" + }, + "record": { + "Text": { + "value": "The quick brown fox." + } + } + } + ] + }`, } } From f9313133275532db499d0714fd81a7c5e505fded Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 14:03:52 +0700 Subject: [PATCH 22/39] formart json --- app_test.go | 1 + app_test_json.go | 80 ++++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/app_test.go b/app_test.go index 8494c9a..0ef1e44 100644 --- a/app_test.go +++ b/app_test.go @@ -101,6 +101,7 @@ func handleResponseRecordsCursor(response http.ResponseWriter, request *http.Req fmt.Fprint(response, testData.output) } } + func handleResponseRecordComments(response http.ResponseWriter, request *http.Request) { checkAuth(response, request) if request.Method == "POST" { diff --git a/app_test_json.go b/app_test_json.go index 2456e47..044e04a 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -22,18 +22,18 @@ func GetTestDataGetRecord() *TestData { "Updated_by": { "type": "MODIFIER", "value": { - "code": "Administrator", - "name": "Administrator" + "code": "Administrator", + "name": "Administrator" }, "key": "hehehe" }, - "$id": { - "type": "__ID__", - "value": "1" - } + "$id": { + "type": "__ID__", + "value": "1" + } } }`, } @@ -91,9 +91,9 @@ func GetDataTestUploadFile() *TestData { "record": { "attached_file": { "value": [ - { - "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" - } + { + "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" + } ] } } @@ -146,14 +146,14 @@ func GetDataTestRecordComments() *TestData { ], "older": false, "newer": false - }`, + }`, } } func GetDataTestForm() *TestData { return &TestData{ output: `{ - "properties": [ + "properties": [ { "code": "string_1", "defaultValue": "", @@ -197,8 +197,8 @@ func GetDataTestForm() *TestData { "required": "false", "type": "CHECK_BOX" } - ] - }`, + ] + }`, } } @@ -211,7 +211,7 @@ func GetTestDataAddRecord() *TestData { return &TestData{ output: `{ "id": "1", - "revision": "1" + "revision": "1" }`, } } @@ -255,23 +255,23 @@ func GetDataTestGetRecordsByCursor() *TestData { input: []interface{}{"9a9716fe-1394-4677-a1c7-2199a5d28215"}, output: `{ "records": [ - { - "$id": { - "type": "__ID__", - "value": "1" - }, - "Created_by": { - "type": "CREATOR", - "value": { - "code": "Administrator", - "name": "Administrator" - } - }, - "Created_datetime": { - "type": "CREATED_TIME", - "value": "2019-05-23T04:50:00Z" - } + { + "$id": { + "type": "__ID__", + "value": "1" + }, + "Created_by": { + "type": "CREATOR", + "value": { + "code": "Administrator", + "name": "Administrator" + } + }, + "Created_datetime": { + "type": "CREATED_TIME", + "value": "2019-05-23T04:50:00Z" } + } ], "next": false }`, @@ -306,24 +306,24 @@ func GetTestDataUpdateRecordByKey() *TestData { "records": [ { "updateKey": { - "field": "unique_key", - "value": "CODE123" + "field": "unique_key", + "value": "CODE123" }, "record": { - "Text": { - "value": "Silver plates" - } + "Text": { + "value": "Silver plates" + } } }, { "updateKey": { - "field": "unique_key", - "value": "CODE456" + "field": "unique_key", + "value": "CODE456" }, "record": { - "Text": { - "value": "The quick brown fox." - } + "Text": { + "value": "The quick brown fox." + } } } ] From ec2ce23998367d05e1d4f05cdd37fb61338f3566 Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 14:12:38 +0700 Subject: [PATCH 23/39] add appid to newAppWithToken --- app_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app_test.go b/app_test.go index 0ef1e44..c24c79f 100644 --- a/app_test.go +++ b/app_test.go @@ -199,6 +199,7 @@ func newAppWithGuest() *App { } func newAppWithToken() *App { return &App{ + AppId: KINTONE_APP_ID, Domain: KINTONE_DOMAIN, ApiToken: KINTONE_API_TOKEN, } From b6de386a488f6d7bf9db7eb7b2faa598b0a86f25 Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 15:05:40 +0700 Subject: [PATCH 24/39] remove kintone api token from app struct --- app_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app_test.go b/app_test.go index c24c79f..d7cecd6 100644 --- a/app_test.go +++ b/app_test.go @@ -64,15 +64,14 @@ func checkAuth(response http.ResponseWriter, request *http.Request) { userAndPass := base64.StdEncoding.EncodeToString( []byte(KINTONE_USERNAME + ":" + KINTONE_USERNAME)) if authPassword != userAndPass && authToken != KINTONE_API_TOKEN { - http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) + http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } } func checkContentType(response http.ResponseWriter, request *http.Request) { contentType := request.Header.Get("Content-Type") if contentType != "application/json" { - http.Error(response, http.StatusText(http.StatusNotFound), http.StatusNotFound) - + http.Error(response, http.StatusText(http.StatusNoContent), http.StatusNoContent) } } @@ -186,7 +185,6 @@ func newApp() *App { User: KINTONE_USERNAME, Password: KINTONE_PASSWORD, AppId: KINTONE_APP_ID, - ApiToken: KINTONE_API_TOKEN, } } func newAppWithGuest() *App { From 8b1f377823a21c5e8fb2fa9d22afdd52e056e3db Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 15:32:20 +0700 Subject: [PATCH 25/39] check auth --- app_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app_test.go b/app_test.go index d7cecd6..7fe373e 100644 --- a/app_test.go +++ b/app_test.go @@ -63,8 +63,11 @@ func checkAuth(response http.ResponseWriter, request *http.Request) { authToken := request.Header.Get(AUTH_HEADER_TOKEN) userAndPass := base64.StdEncoding.EncodeToString( []byte(KINTONE_USERNAME + ":" + KINTONE_USERNAME)) - if authPassword != userAndPass && authToken != KINTONE_API_TOKEN { + if authToken != KINTONE_API_TOKEN { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + } else if authPassword != userAndPass { + http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + } } From c137fd1c766784358e25ee2df9371946dd104bce Mon Sep 17 00:00:00 2001 From: huong Date: Mon, 6 Jan 2020 16:08:37 +0700 Subject: [PATCH 26/39] change variable app --- app_test.go | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/app_test.go b/app_test.go index 7fe373e..1b9a20c 100644 --- a/app_test.go +++ b/app_test.go @@ -63,11 +63,10 @@ func checkAuth(response http.ResponseWriter, request *http.Request) { authToken := request.Header.Get(AUTH_HEADER_TOKEN) userAndPass := base64.StdEncoding.EncodeToString( []byte(KINTONE_USERNAME + ":" + KINTONE_USERNAME)) - if authToken != KINTONE_API_TOKEN { + if authToken != "" && authToken != KINTONE_API_TOKEN { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } else if authPassword != userAndPass { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) - } } @@ -243,8 +242,8 @@ func TestAddRecord(t *testing.T) { } func TestGetRecord(t *testing.T) { testData := GetTestDataGetRecord() - a := newApp() - if rec, err := a.GetRecord(uint64(testData.input[0].(int))); err != nil { + app := newApp() + if rec, err := app.GetRecord(uint64(testData.input[0].(int))); err != nil { t.Error(err) } else { if rec.Id() != 1 { @@ -255,7 +254,7 @@ func TestGetRecord(t *testing.T) { if len(files) == 0 { continue } - fd, err := a.Download(files[0].FileKey) + fd, err := app.Download(files[0].FileKey) if err != nil { t.Error(err) } else { @@ -266,7 +265,7 @@ func TestGetRecord(t *testing.T) { } } - if recs, err := a.GetRecords(nil, "limit 3 offset 3"); err != nil { + if recs, err := app.GetRecords(nil, "limit 3 offset 3"); err != nil { t.Error(err) } else { if len(recs) > 3 { @@ -274,7 +273,7 @@ func TestGetRecord(t *testing.T) { } } - if recs, err := a.GetAllRecords([]string{"レコード番号"}); err != nil { + if recs, err := app.GetAllRecords([]string{"レコード番号"}); err != nil { t.Error(err) } else { t.Log(len(recs)) @@ -283,14 +282,14 @@ func TestGetRecord(t *testing.T) { } func TestUpdateRecord(t *testing.T) { testData := GetTestDataGetRecord() - a := newApp() + app := newApp() - rec, err := a.GetRecord(uint64(testData.input[0].(int))) + rec, err := app.GetRecord(uint64(testData.input[0].(int))) if err != nil { t.Fatal(err) } rec.Fields["title"] = SingleLineTextField("new title") - if err := a.UpdateRecord(rec, true); err != nil { + if err := app.UpdateRecord(rec, true); err != nil { t.Error("UpdateRecord failed", err) } @@ -298,11 +297,11 @@ func TestUpdateRecord(t *testing.T) { "field": "unique_key", "value": "unique_code" }`) - if err := a.UpdateRecordByKey(rec, true, "key"); err != nil { + if err := app.UpdateRecordByKey(rec, true, "key"); err != nil { t.Error("UpdateRecordByKey failed", err) } - recs, err := a.GetRecords(nil, "limit 3") + recs, err := app.GetRecords(nil, "limit 3") if err != nil { t.Fatal(err) } @@ -314,20 +313,20 @@ func TestUpdateRecord(t *testing.T) { "value": "unique_code" }`) } - if err := a.UpdateRecords(recs, true); err != nil { + if err := app.UpdateRecords(recs, true); err != nil { t.Error("UpdateRecords failed", err) } - if err := a.UpdateRecordsByKey(recs, true, "key"); err != nil { + if err := app.UpdateRecordsByKey(recs, true, "key"); err != nil { t.Error("UpdateRecordsByKey failed", err) } } func TestDeleteRecord(t *testing.T) { - a := newApp() + app := newApp() ids := []uint64{6, 7} - if err := a.DeleteRecords(ids); err != nil { + if err := app.DeleteRecords(ids); err != nil { t.Error("DeleteRecords failed", err) } } @@ -361,9 +360,9 @@ func TestCreateCursor(t *testing.T) { } func TestFields(t *testing.T) { - a := newApp() + app := newApp() - fi, err := a.Fields() + fi, err := app.Fields() if err != nil { t.Error("Fields failed", err) } @@ -373,27 +372,27 @@ func TestFields(t *testing.T) { } func TestApiToken(t *testing.T) { - a := newAppWithToken() - _, err := a.Fields() + app := newAppWithToken() + _, err := app.Fields() if err != nil { t.Error("Api token failed", err) } } func TestGuestSpace(t *testing.T) { - a := newAppWithGuest() + app := newAppWithGuest() - _, err := a.Fields() + _, err := app.Fields() if err != nil { t.Error("GuestSpace failed", err) } } func TestGetRecordComments(t *testing.T) { - a := newApp() + app := newApp() var offset uint64 = 0 var limit uint64 = 10 - if rec, err := a.GetRecordComments(1, "asc", offset, limit); err != nil { + if rec, err := app.GetRecordComments(1, "asc", offset, limit); err != nil { t.Error(err) } else { if !strings.Contains(rec[0].Id, "3") { From add223330c1212fa6932571c4835566017e5703c Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 09:19:07 +0700 Subject: [PATCH 27/39] fix review #6 --- app_test.go | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/app_test.go b/app_test.go index 1b9a20c..ab231a3 100644 --- a/app_test.go +++ b/app_test.go @@ -27,6 +27,8 @@ const ( KINTONE_GUEST_SPACE_ID = 1 AUTH_HEADER_TOKEN = "X-Cybozu-API-Token" AUTH_HEADER_PASSWORD = "X-Cybozu-Authorization" + CONTENT_TYPE = "Content-Type" + APPLICATION_JSON = "application/json" ) func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { @@ -44,7 +46,7 @@ func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { return ts, nil } -func createServerMux() (*http.ServeMux, error) { +func createServerMux() *http.ServeMux { mux := http.NewServeMux() mux.HandleFunc("/k/v1/record.json", handleResponseGetRecord) mux.HandleFunc("/k/v1/records.json", handleResponseGetRecords) @@ -54,7 +56,7 @@ func createServerMux() (*http.ServeMux, error) { mux.HandleFunc("/k/v1/records/cursor.json", handleResponseRecordsCursor) mux.HandleFunc("/k/v1/form.json", handleResponseForm) mux.HandleFunc("/k/guest/1/v1/form.json", handleResponseForm) - return mux, nil + return mux } // header check @@ -68,11 +70,10 @@ func checkAuth(response http.ResponseWriter, request *http.Request) { } else if authPassword != userAndPass { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } - } func checkContentType(response http.ResponseWriter, request *http.Request) { - contentType := request.Header.Get("Content-Type") - if contentType != "application/json" { + contentType := request.Header.Get(CONTENT_TYPE) + if contentType != APPLICATION_JSON { http.Error(response, http.StatusText(http.StatusNoContent), http.StatusNoContent) } } @@ -105,12 +106,11 @@ func handleResponseRecordsCursor(response http.ResponseWriter, request *http.Req func handleResponseRecordComments(response http.ResponseWriter, request *http.Request) { checkAuth(response, request) + checkContentType(response, request) if request.Method == "POST" { - checkContentType(response, request) testData := GetTestDataAddRecordComment() fmt.Fprint(response, testData.output) } else if request.Method == "DELETE" { - checkContentType(response, request) testData := GetDataTestDeleteRecordComment() fmt.Fprint(response, testData.output) } @@ -126,16 +126,14 @@ func handleResponseUploadFile(response http.ResponseWriter, request *http.Reques func handleResponseGetRecord(response http.ResponseWriter, request *http.Request) { checkAuth(response, request) + checkContentType(response, request) if request.Method == "GET" { - checkContentType(response, request) testData := GetTestDataGetRecord() fmt.Fprint(response, testData.output) } else if request.Method == "PUT" { - checkContentType(response, request) testData := GetTestDataUpdateRecordByKey() fmt.Fprint(response, testData.output) } else if request.Method == "POST" { - checkContentType(response, request) testData := GetTestDataAddRecord() fmt.Fprint(response, testData.output) } @@ -144,16 +142,14 @@ func handleResponseGetRecord(response http.ResponseWriter, request *http.Request func handleResponseGetRecords(response http.ResponseWriter, request *http.Request) { checkAuth(response, request) + checkContentType(response, request) if request.Method == "GET" { - checkContentType(response, request) testData := GetTestDataGetRecords() fmt.Fprint(response, testData.output) } else if request.Method == "DELETE" { - checkContentType(response, request) testData := GetTestDataDeleteRecords() fmt.Fprint(response, testData.output) } else if request.Method == "POST" { - checkContentType(response, request) testData := GetTestDataAddRecords() fmt.Fprint(response, testData.output) } @@ -169,10 +165,7 @@ func handleResponseGetRecordsComments(response http.ResponseWriter, request *htt } func TestMain(m *testing.M) { - mux, err := createServerMux() - if err != nil { - fmt.Println("StartServerTest", err) - } + mux := createServerMux() ts, err := createServerTest(mux) if err != nil { fmt.Println("createServerTest", err) @@ -193,7 +186,8 @@ func newAppWithGuest() *App { return &App{ Domain: KINTONE_DOMAIN, AppId: KINTONE_APP_ID, - ApiToken: KINTONE_API_TOKEN, + User: KINTONE_USERNAME, + Password: KINTONE_PASSWORD, GuestSpaceId: KINTONE_GUEST_SPACE_ID, } } @@ -265,7 +259,7 @@ func TestGetRecord(t *testing.T) { } } - if recs, err := app.GetRecords(nil, "limit 3 offset 3"); err != nil { + if recs, err := app.GetRecords(nil, testData.input[0].(string)); err != nil { t.Error(err) } else { if len(recs) > 3 { From f6bd0be82311196879571fb15a9610b62d226146 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 09:19:26 +0700 Subject: [PATCH 28/39] add json input --- app_test_json.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app_test_json.go b/app_test_json.go index 044e04a..934a0c3 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -16,7 +16,7 @@ func GetTestDataDeleteRecords() *TestData { } func GetTestDataGetRecord() *TestData { return &TestData{ - input: []interface{}{1}, + input: []interface{}{"limit 100"}, output: `{ "record": { "Updated_by": { @@ -41,9 +41,13 @@ func GetTestDataGetRecord() *TestData { func GetTestDataGetRecords() *TestData { return &TestData{ + input: []interface{}{ + nil, + "limit 3 offset 3", + }, output: `{ - "records": [ - { + "records": [ + { "Created_datetime": { "type": "CREATED_TIME", "value": "2019-03-11T04:50:00Z" From 2e8448f8e9baa4b804916db633e523f96e4c7df2 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 09:46:15 +0700 Subject: [PATCH 29/39] formart json --- app_test_json.go | 465 ++++++++++++++++++++++++----------------------- 1 file changed, 236 insertions(+), 229 deletions(-) diff --git a/app_test_json.go b/app_test_json.go index 934a0c3..215cfee 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -16,26 +16,24 @@ func GetTestDataDeleteRecords() *TestData { } func GetTestDataGetRecord() *TestData { return &TestData{ - input: []interface{}{"limit 100"}, - output: `{ - "record": { - "Updated_by": { - "type": "MODIFIER", - "value": { - "code": "Administrator", - "name": "Administrator" - }, - "key": "hehehe" - - - }, - - "$id": { - "type": "__ID__", - "value": "1" - } - } - }`, + input: []interface{}{1}, + output: ` + { + "record":{ + "Updated_by":{ + "type":"MODIFIER", + "value":{ + "code":"Administrator", + "name":"Administrator" + }, + "key":"hehehe" + }, + "$id":{ + "type":"__ID__", + "value":"1" + } + } + }`, } } @@ -45,164 +43,168 @@ func GetTestDataGetRecords() *TestData { nil, "limit 3 offset 3", }, - output: `{ - "records": [ - { - "Created_datetime": { - "type": "CREATED_TIME", - "value": "2019-03-11T04:50:00Z" - }, - "Created_by": { - "type": "CREATOR", - "value": { - "code": "Administrator", - "name": "Administrator" - } - }, - "$id": { - "type": "__ID__", - "value": "1" - } - }, - { - "Created_datetime": { - "type": "CREATED_TIME", - "value": "2019-03-11T06:42:00Z" - }, - "Created_by": { - "type": "CREATOR", - "value": { - "code": "Administrator", - "name": "Administrator" - } - }, - "$id": { - "type": "__ID__", - "value": "2" - } - } - ], - "totalCount": null - }`, + output: ` + { + "records":[ + { + "Created_datetime":{ + "type":"CREATED_TIME", + "value":"2019-03-11T04:50:00Z" + }, + "Created_by":{ + "type":"CREATOR", + "value":{ + "code":"Administrator", + "name":"Administrator" + } + }, + "$id":{ + "type":"__ID__", + "value":"1" + } + }, + { + "Created_datetime":{ + "type":"CREATED_TIME", + "value":"2019-03-11T06:42:00Z" + }, + "Created_by":{ + "type":"CREATOR", + "value":{ + "code":"Administrator", + "name":"Administrator" + } + }, + "$id":{ + "type":"__ID__", + "value":"2" + } + } + ], + "totalCount":null + }`, } } func GetDataTestUploadFile() *TestData { return &TestData{ - output: `{ - "app": 3, - "id": 6, - "record": { - "attached_file": { - "value": [ - { - "fileKey": " c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" - } - ] - } - } - }`, + output: ` + { + "app":3, + "id":6, + "record":{ + "attached_file":{ + "value":[ + { + "fileKey":" c15b3870-7505-4ab6-9d8d-b9bdbc74f5d6" + } + ] + } + } + }`, } } func GetDataTestRecordComments() *TestData { return &TestData{ - output: `{ - "comments": [ - { - "id": "3", - "text": "user14 Thank you! Looks great.", - "createdAt": "2016-05-09T18:29:05Z", - "creator": { - "code": "user13", - "name": "user13" - }, - "mentions": [ - { - "code": "user14", - "type": "USER" - } - ] - }, - { - "id": "2", - "text": "user13 Global Sales APAC Taskforce \nHere is today's report.", - "createdAt": "2016-05-09T18:27:54Z", - "creator": { - "code": "user14", - "name": "user14" - }, - "mentions": [ - { - "code": "user13", - "type": "USER" - }, - { - "code": "Global Sales_1BNZeQ", - "type": "ORGANIZATION" - }, - { - "code": "APAC Taskforce_DJrvzu", - "type": "GROUP" - } - ] - } - ], - "older": false, - "newer": false - }`, + output: ` + { + "comments":[ + { + "id":"3", + "text":"user14 Thank you! Looks great.", + "createdAt":"2016-05-09T18:29:05Z", + "creator":{ + "code":"user13", + "name":"user13" + }, + "mentions":[ + { + "code":"user14", + "type":"USER" + } + ] + }, + { + "id":"2", + "text":"user13 Global Sales APAC Taskforce \nHere is today's report.", + "createdAt":"2016-05-09T18:27:54Z", + "creator":{ + "code":"user14", + "name":"user14" + }, + "mentions":[ + { + "code":"user13", + "type":"USER" + }, + { + "code":"Global Sales_1BNZeQ", + "type":"ORGANIZATION" + }, + { + "code":"APAC Taskforce_DJrvzu", + "type":"GROUP" + } + ] + } + ], + "older":false, + "newer":false + }`, } } func GetDataTestForm() *TestData { return &TestData{ - output: `{ - "properties": [ - { - "code": "string_1", - "defaultValue": "", - "expression": "", - "hideExpression": "false", - "maxLength": "64", - "minLength": null, - "label": "string_1", - "noLabel": "false", - "required": "true", - "type": "SINGLE_LINE_TEXT", - "unique": "true" - }, - { - "code": "number_1", - "defaultValue": "12345", - "digit": "true", - "displayScale": "4", - "expression": "", - "maxValue": null, - "minValue": null, - "label": "number_1", - "noLabel": "true", - "required": "false", - "type": "NUMBER", - "unique": "false" - }, - { - "code": "checkbox_1", - "defaultValue": [ - "sample1", - "sample3" - ], - "label": "checkbox_1", - "noLabel": "false", - "options": [ - "sample1", - "sample2", - "sample3" - ], - "required": "false", - "type": "CHECK_BOX" - } - ] - }`, + output: ` + { + "properties":[ + { + "code":"string_1", + "defaultValue":"", + "expression":"", + "hideExpression":"false", + "maxLength":"64", + "minLength":null, + "label":"string_1", + "noLabel":"false", + "required":"true", + "type":"SINGLE_LINE_TEXT", + "unique":"true" + }, + { + "code":"number_1", + "defaultValue":"12345", + "digit":"true", + "displayScale":"4", + "expression":"", + "maxValue":null, + "minValue":null, + "label":"number_1", + "noLabel":"true", + "required":"false", + "type":"NUMBER", + "unique":"false" + }, + { + "code":"checkbox_1", + "defaultValue":[ + "sample1", + "sample3" + ], + "label":"checkbox_1", + "noLabel":"false", + "options":[ + "sample1", + "sample2", + "sample3" + ], + "required":"false", + "type":"CHECK_BOX" + } + ] + }`, } } @@ -214,18 +216,19 @@ func GetDataTestDeleteRecordComment() *TestData { func GetTestDataAddRecord() *TestData { return &TestData{ output: `{ - "id": "1", - "revision": "1" - }`, + "id": "1", + "revision": "1" + }`, } } func GetTestDataAddRecords() *TestData { return &TestData{ - output: `{ - "ids": ["77","78"], - "revisions": ["1","1"] - }`, + output: ` + { + "ids": ["77","78"], + "revisions": ["1","1"] + }`, } } @@ -234,22 +237,24 @@ func GetDataTestAddRecord() *TestData { input: []interface{}{ "ほげ春巻.txta", bytes.NewReader([]byte(`abc - hoge - `)), + hoge + `)), "text/html", }, - output: `{ - "id": "1", - "revision": "1" - }`, + output: ` + { + "id": "1", + "revision": "1" + }`, } } func getDataTestCreateCursor() *TestData { return &TestData{ - output: `{ - "id": "9a9716fe-1394-4677-a1c7-2199a5d28215", - "totalCount": 123456 - }`, + output: ` + { + "id": "9a9716fe-1394-4677-a1c7-2199a5d28215", + "totalCount": 123456 + }`, } } @@ -257,28 +262,29 @@ func GetDataTestGetRecordsByCursor() *TestData { return &TestData{ input: []interface{}{"9a9716fe-1394-4677-a1c7-2199a5d28215"}, - output: `{ - "records": [ - { - "$id": { - "type": "__ID__", - "value": "1" - }, - "Created_by": { - "type": "CREATOR", - "value": { - "code": "Administrator", - "name": "Administrator" - } - }, - "Created_datetime": { - "type": "CREATED_TIME", - "value": "2019-05-23T04:50:00Z" - } - } - ], - "next": false - }`, + output: ` + { + "records":[ + { + "$id":{ + "type":"__ID__", + "value":"1" + }, + "Created_by":{ + "type":"CREATOR", + "value":{ + "code":"Administrator", + "name":"Administrator" + } + }, + "Created_datetime":{ + "type":"CREATED_TIME", + "value":"2019-05-23T04:50:00Z" + } + } + ], + "next":false + }`, } } @@ -305,32 +311,33 @@ func GetTestDataAddRecordComment() *TestData { func GetTestDataUpdateRecordByKey() *TestData { return &TestData{ input: []interface{}{2}, - output: `{ - "app": 1, - "records": [ - { - "updateKey": { - "field": "unique_key", - "value": "CODE123" - }, - "record": { - "Text": { - "value": "Silver plates" - } - } - }, - { - "updateKey": { - "field": "unique_key", - "value": "CODE456" - }, - "record": { - "Text": { - "value": "The quick brown fox." - } - } - } - ] - }`, + output: ` + { + "app":1, + "records":[ + { + "updateKey":{ + "field":"unique_key", + "value":"CODE123" + }, + "record":{ + "Text":{ + "value":"Silver plates" + } + } + }, + { + "updateKey":{ + "field":"unique_key", + "value":"CODE456" + }, + "record":{ + "Text":{ + "value":"The quick brown fox." + } + } + } + ] + }`, } } From ae0cda2ea1879c0b7f167d511c977584ff055f0b Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 10:30:24 +0700 Subject: [PATCH 30/39] add basic auth and add input json for something function --- app_test.go | 60 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/app_test.go b/app_test.go index ab231a3..f1f706c 100644 --- a/app_test.go +++ b/app_test.go @@ -27,8 +27,12 @@ const ( KINTONE_GUEST_SPACE_ID = 1 AUTH_HEADER_TOKEN = "X-Cybozu-API-Token" AUTH_HEADER_PASSWORD = "X-Cybozu-Authorization" + AUTH_HEADER_BASIC = "Authorization" CONTENT_TYPE = "Content-Type" APPLICATION_JSON = "application/json" + BASIC_AUTH = true + BASIC_AUTH_USER = "basic" + BASIC_AUTH_PASSWORD = "basic" ) func createServerTest(mux *http.ServeMux) (*httptest.Server, error) { @@ -63,14 +67,28 @@ func createServerMux() *http.ServeMux { func checkAuth(response http.ResponseWriter, request *http.Request) { authPassword := request.Header.Get(AUTH_HEADER_PASSWORD) authToken := request.Header.Get(AUTH_HEADER_TOKEN) + authBasic := request.Header.Get(AUTH_HEADER_BASIC) + userAndPass := base64.StdEncoding.EncodeToString( []byte(KINTONE_USERNAME + ":" + KINTONE_USERNAME)) + + userAndPassBasic := "Basic " + base64.StdEncoding.EncodeToString( + []byte(BASIC_AUTH_USER+":"+BASIC_AUTH_PASSWORD)) + + if authToken != "" && authPassword != "" && authBasic != "" { + http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + } + if BASIC_AUTH && authBasic != userAndPassBasic { + http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + } if authToken != "" && authToken != KINTONE_API_TOKEN { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) - } else if authPassword != userAndPass { + } + if authPassword != "" && authPassword != userAndPass { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } } + func checkContentType(response http.ResponseWriter, request *http.Request) { contentType := request.Header.Get(CONTENT_TYPE) if contentType != APPLICATION_JSON { @@ -236,6 +254,7 @@ func TestAddRecord(t *testing.T) { } func TestGetRecord(t *testing.T) { testData := GetTestDataGetRecord() + testDataRecords := GetTestDataGetRecords() app := newApp() if rec, err := app.GetRecord(uint64(testData.input[0].(int))); err != nil { t.Error(err) @@ -259,7 +278,7 @@ func TestGetRecord(t *testing.T) { } } - if recs, err := app.GetRecords(nil, testData.input[0].(string)); err != nil { + if recs, err := app.GetRecords(testDataRecords.input[0].([]string), testDataRecords.input[1].(string)); err != nil { t.Error(err) } else { if len(recs) > 3 { @@ -267,7 +286,7 @@ func TestGetRecord(t *testing.T) { } } - if recs, err := app.GetAllRecords([]string{"レコード番号"}); err != nil { + if recs, err := app.GetAllRecords(testDataRecords.input[0].([]string)); err != nil { t.Error(err) } else { t.Log(len(recs)) @@ -276,6 +295,9 @@ func TestGetRecord(t *testing.T) { } func TestUpdateRecord(t *testing.T) { testData := GetTestDataGetRecord() + testDataRecords := GetTestDataGetRecords() + testDataRecordByKey := GetTestDataUpdateRecordByKey() + app := newApp() rec, err := app.GetRecord(uint64(testData.input[0].(int))) @@ -283,19 +305,18 @@ func TestUpdateRecord(t *testing.T) { t.Fatal(err) } rec.Fields["title"] = SingleLineTextField("new title") - if err := app.UpdateRecord(rec, true); err != nil { + if err := app.UpdateRecord(rec, testData.input[1].(bool)); err != nil { t.Error("UpdateRecord failed", err) } - rec.Fields["key"] = SingleLineTextField(` { + rec.Fields[testDataRecordByKey.input[1].(string)] = SingleLineTextField(` { "field": "unique_key", "value": "unique_code" }`) - if err := app.UpdateRecordByKey(rec, true, "key"); err != nil { - + if err := app.UpdateRecordByKey(rec, testData.input[1].(bool), testDataRecordByKey.input[1].(string)); err != nil { t.Error("UpdateRecordByKey failed", err) } - recs, err := app.GetRecords(nil, "limit 3") + recs, err := app.GetRecords(testDataRecords.input[0].([]string), testDataRecords.input[1].(string)) if err != nil { t.Fatal(err) } @@ -307,20 +328,19 @@ func TestUpdateRecord(t *testing.T) { "value": "unique_code" }`) } - if err := app.UpdateRecords(recs, true); err != nil { + if err := app.UpdateRecords(recs, testData.input[1].(bool)); err != nil { t.Error("UpdateRecords failed", err) } - if err := app.UpdateRecordsByKey(recs, true, "key"); err != nil { + if err := app.UpdateRecordsByKey(recs, testDataRecordByKey.input[2].(bool), testDataRecordByKey.input[1].(string)); err != nil { t.Error("UpdateRecordsByKey failed", err) } } func TestDeleteRecord(t *testing.T) { + testData := GetTestDataDeleteRecords() app := newApp() - - ids := []uint64{6, 7} - if err := app.DeleteRecords(ids); err != nil { + if err := app.DeleteRecords(testData.input[0].([]uint64)); err != nil { t.Error("DeleteRecords failed", err) } } @@ -355,7 +375,6 @@ func TestCreateCursor(t *testing.T) { func TestFields(t *testing.T) { app := newApp() - fi, err := app.Fields() if err != nil { t.Error("Fields failed", err) @@ -375,7 +394,6 @@ func TestApiToken(t *testing.T) { func TestGuestSpace(t *testing.T) { app := newAppWithGuest() - _, err := app.Fields() if err != nil { t.Error("GuestSpace failed", err) @@ -383,10 +401,9 @@ func TestGuestSpace(t *testing.T) { } func TestGetRecordComments(t *testing.T) { + testData := GetDataTestRecordComments() app := newApp() - var offset uint64 = 0 - var limit uint64 = 10 - if rec, err := app.GetRecordComments(1, "asc", offset, limit); err != nil { + if rec, err := app.GetRecordComments(uint64(testData.input[0].(int)), testData.input[1].(string), uint64(testData.input[2].(int)), uint64(testData.input[3].(int))); err != nil { t.Error(err) } else { if !strings.Contains(rec[0].Id, "3") { @@ -414,13 +431,12 @@ func TestAddRecordComment(t *testing.T) { } func TestDeleteComment(t *testing.T) { + testData := GetDataTestDeleteRecordComment() appTest := newApp() - var cmtID uint64 = 14 - err := appTest.DeleteComment(3, 12) - + err := appTest.DeleteComment(uint64(testData.input[0].(int)), uint64(testData.input[1].(int))) if err != nil { t.Error(err) } else { - t.Logf("The comment with id = %v has been deleted successefully!", cmtID) + t.Logf("The comment with id = %v has been deleted successefully!", uint64(testData.input[0].(int))) } } From 9d87cf5bce991d56916885979dc5de2abdae4712 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 10:30:35 +0700 Subject: [PATCH 31/39] add input json --- app_test_json.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app_test_json.go b/app_test_json.go index 215cfee..d390786 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -11,12 +11,13 @@ type TestData struct { func GetTestDataDeleteRecords() *TestData { return &TestData{ + input: []interface{}{[]uint64{6, 7}}, output: `{}`, } } func GetTestDataGetRecord() *TestData { return &TestData{ - input: []interface{}{1}, + input: []interface{}{1, true}, output: ` { "record":{ @@ -107,6 +108,7 @@ func GetDataTestUploadFile() *TestData { func GetDataTestRecordComments() *TestData { return &TestData{ + input: []interface{}{1, "asc", 0, 10}, output: ` { "comments":[ @@ -210,6 +212,7 @@ func GetDataTestForm() *TestData { func GetDataTestDeleteRecordComment() *TestData { return &TestData{ + input: []interface{}{3, 14}, output: `{}`, } } @@ -310,7 +313,7 @@ func GetTestDataAddRecordComment() *TestData { } func GetTestDataUpdateRecordByKey() *TestData { return &TestData{ - input: []interface{}{2}, + input: []interface{}{2, "key", true}, output: ` { "app":1, From 882ae7ebcfabf724616953e98f98a45670721c28 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 11:04:12 +0700 Subject: [PATCH 32/39] fix unit test --- app_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app_test.go b/app_test.go index f1f706c..6e59862 100644 --- a/app_test.go +++ b/app_test.go @@ -75,10 +75,10 @@ func checkAuth(response http.ResponseWriter, request *http.Request) { userAndPassBasic := "Basic " + base64.StdEncoding.EncodeToString( []byte(BASIC_AUTH_USER+":"+BASIC_AUTH_PASSWORD)) - if authToken != "" && authPassword != "" && authBasic != "" { + if authToken == "" && authPassword == "" && authBasic == "" { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } - if BASIC_AUTH && authBasic != userAndPassBasic { + if BASIC_AUTH && authBasic != "" && authBasic != userAndPassBasic { http.Error(response, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } if authToken != "" && authToken != KINTONE_API_TOKEN { From c8265c66db80582cba755f703b1bb7032b11e645 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 11:04:31 +0700 Subject: [PATCH 33/39] fix value json get test data records --- app_test_json.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_test_json.go b/app_test_json.go index d390786..34b20d9 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -41,7 +41,7 @@ func GetTestDataGetRecord() *TestData { func GetTestDataGetRecords() *TestData { return &TestData{ input: []interface{}{ - nil, + []string{"Created_datetime"}, "limit 3 offset 3", }, output: ` From 958b54ec38a5650f45ad73c78633fc4a120da0e7 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 11:19:38 +0700 Subject: [PATCH 34/39] remove filed data test --- app_test_json.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_test_json.go b/app_test_json.go index 34b20d9..70e2e40 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -41,7 +41,7 @@ func GetTestDataGetRecord() *TestData { func GetTestDataGetRecords() *TestData { return &TestData{ input: []interface{}{ - []string{"Created_datetime"}, + []string{}, "limit 3 offset 3", }, output: ` From b80c04c546819dbe03c7176e1229a9f5875c0e58 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 12:58:43 +0700 Subject: [PATCH 35/39] fix review --- app_test.go | 7 +++++-- cursor_test.go | 5 +---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app_test.go b/app_test.go index 6e59862..01b7004 100644 --- a/app_test.go +++ b/app_test.go @@ -13,6 +13,7 @@ import ( "net" "net/http" "net/http/httptest" + "os" "strings" "testing" "time" @@ -186,10 +187,12 @@ func TestMain(m *testing.M) { mux := createServerMux() ts, err := createServerTest(mux) if err != nil { - fmt.Println("createServerTest", err) + fmt.Println("createServerTest: ", err) + os.Exit(1) } - m.Run() + code := m.Run() ts.Close() + os.Exit(code) } func newApp() *App { diff --git a/cursor_test.go b/cursor_test.go index c7a207c..112bb24 100644 --- a/cursor_test.go +++ b/cursor_test.go @@ -1,16 +1,13 @@ package kintone import ( - "fmt" "testing" ) func TestDecodeCursor(t *testing.T) { data := []byte(`{"id":"aaaaaaaaaaaaaaaaaa","totalCount":"null"}`) - cursor, err := decodeCursor(data) + _, err := decodeCursor(data) if err != nil { t.Errorf("TestDecodeCursor is failed: %v", err) - } - fmt.Println(cursor) } From 3a33005ecf312cfdc520d65bccd7a281ffb84d99 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 13:02:08 +0700 Subject: [PATCH 36/39] rm space --- app_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/app_test.go b/app_test.go index 01b7004..78b2b47 100644 --- a/app_test.go +++ b/app_test.go @@ -156,7 +156,6 @@ func handleResponseGetRecord(response http.ResponseWriter, request *http.Request testData := GetTestDataAddRecord() fmt.Fprint(response, testData.output) } - } func handleResponseGetRecords(response http.ResponseWriter, request *http.Request) { From 309fa8dad24d5573e7a03405d8fd1fcb6e44d573 Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 13:20:07 +0700 Subject: [PATCH 37/39] remove space --- app_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/app_test.go b/app_test.go index 78b2b47..ac05fa9 100644 --- a/app_test.go +++ b/app_test.go @@ -171,7 +171,6 @@ func handleResponseGetRecords(response http.ResponseWriter, request *http.Reques testData := GetTestDataAddRecords() fmt.Fprint(response, testData.output) } - } func handleResponseGetRecordsComments(response http.ResponseWriter, request *http.Request) { @@ -179,7 +178,6 @@ func handleResponseGetRecordsComments(response http.ResponseWriter, request *htt checkContentType(response, request) testData := GetDataTestRecordComments() fmt.Fprint(response, testData.output) - } func TestMain(m *testing.M) { From eefdd0f96d76d50ff2a5bbbe331ba1a06c9dd6ee Mon Sep 17 00:00:00 2001 From: huong Date: Tue, 7 Jan 2020 14:22:02 +0700 Subject: [PATCH 38/39] unit test get process --- app_test.go | 16 +++++++++++ app_test_json.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/app_test.go b/app_test.go index ac05fa9..c1fe851 100644 --- a/app_test.go +++ b/app_test.go @@ -59,6 +59,7 @@ func createServerMux() *http.ServeMux { mux.HandleFunc("/k/v1/file.json", handleResponseUploadFile) mux.HandleFunc("/k/v1/record/comment.json", handleResponseRecordComments) mux.HandleFunc("/k/v1/records/cursor.json", handleResponseRecordsCursor) + mux.HandleFunc("/k/v1/app/status.json", handleResponseProcess) mux.HandleFunc("/k/v1/form.json", handleResponseForm) mux.HandleFunc("/k/guest/1/v1/form.json", handleResponseForm) return mux @@ -98,6 +99,12 @@ func checkContentType(response http.ResponseWriter, request *http.Request) { } // handler mux +func handleResponseProcess(response http.ResponseWriter, request *http.Request) { + checkAuth(response, request) + TestData := GetTestDataProcess() + fmt.Fprint(response, TestData.output) +} + func handleResponseForm(response http.ResponseWriter, request *http.Request) { checkAuth(response, request) if request.Method == "GET" { @@ -440,3 +447,12 @@ func TestDeleteComment(t *testing.T) { t.Logf("The comment with id = %v has been deleted successefully!", uint64(testData.input[0].(int))) } } + +func TestGetProcess(t *testing.T) { + TestData := GetTestDataProcess() + app := newApp() + _, err := app.GetProcess(TestData.input[0].(string)) + if err != nil { + t.Error("TestGetProcess failed: ", err) + } +} diff --git a/app_test_json.go b/app_test_json.go index 70e2e40..100b258 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -9,6 +9,81 @@ type TestData struct { output string } +func GetTestDataProcess() *TestData { + return &TestData{ + input: []interface{}{"en"}, + output: ` + { + "enable": true, + "states": { + "Not started": { + "name": "Not started", + "index": "0", + "assignee": { + "type": "ONE", + "entities": [ + ] + } + }, + "In progress": { + "name": "In progress", + "index": "1", + "assignee": { + "type": "ALL", + "entities": [ + { + "entity": { + "type": "USER", + "code": "user1" + }, + "includeSubs": false + }, + { + "entity": { + "type": "FIELD_ENTITY", + "code": "creator" + }, + "includeSubs": false + }, + { + "entity": { + "type": "CUSTOM_FIELD", + "code": "Boss" + }, + "includeSubs": false + } + ] + } + }, + "Completed": { + "name": "Completed", + "index": "2", + "assignee": { + "type": "ONE", + "entities": [ + ] + } + } + }, + "actions": [ + { + "name": "Start", + "from": "Not started", + "to": "In progress", + "filterCond": "Record_number = \"1\"" + }, + { + "name": "Complete", + "from": "In progress", + "to": "Completed", + "filterCond": "" + } + ], + "revision": "3" + }`, + } +} + func GetTestDataDeleteRecords() *TestData { return &TestData{ input: []interface{}{[]uint64{6, 7}}, From bbfba1120ca7bb3922dec6e36753e36e56df1a21 Mon Sep 17 00:00:00 2001 From: huong Date: Wed, 8 Jan 2020 09:24:25 +0700 Subject: [PATCH 39/39] formart space --- app_test_json.go | 122 +++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 62 deletions(-) diff --git a/app_test_json.go b/app_test_json.go index 100b258..f38d07d 100644 --- a/app_test_json.go +++ b/app_test_json.go @@ -14,72 +14,70 @@ func GetTestDataProcess() *TestData { input: []interface{}{"en"}, output: ` { - "enable": true, - "states": { - "Not started": { - "name": "Not started", - "index": "0", - "assignee": { - "type": "ONE", - "entities": [ - ] - } - }, - "In progress": { - "name": "In progress", - "index": "1", - "assignee": { - "type": "ALL", - "entities": [ - { - "entity": { - "type": "USER", - "code": "user1" - }, - "includeSubs": false - }, - { - "entity": { - "type": "FIELD_ENTITY", - "code": "creator" - }, - "includeSubs": false - }, - { - "entity": { - "type": "CUSTOM_FIELD", - "code": "Boss" - }, - "includeSubs": false - } - ] - } - }, - "Completed": { - "name": "Completed", - "index": "2", - "assignee": { - "type": "ONE", - "entities": [ - ] + "enable":true, + "states":{ + "Not started":{ + "name":"Not started", + "index":"0", + "assignee":{ + "type":"ONE", + "entities":[] + } + }, + "In progress":{ + "name":"In progress", + "index":"1", + "assignee":{ + "type":"ALL", + "entities":[ + { + "entity":{ + "type":"USER", + "code":"user1" + }, + "includeSubs":false + }, + { + "entity":{ + "type":"FIELD_ENTITY", + "code":"creator" + }, + "includeSubs":false + }, + { + "entity":{ + "type":"CUSTOM_FIELD", + "code":"Boss" + }, + "includeSubs":false } + ] } - }, - "actions": [ - { - "name": "Start", - "from": "Not started", - "to": "In progress", - "filterCond": "Record_number = \"1\"" - }, - { - "name": "Complete", - "from": "In progress", - "to": "Completed", - "filterCond": "" + }, + "Completed":{ + "name":"Completed", + "index":"2", + "assignee":{ + "type":"ONE", + "entities":[] } + } + }, + "actions":[ + { + "name":"Start", + "from":"Not started", + "to":"In progress", + "filterCond":"Record_number = \"1\"" + }, + { + "name":"Complete", + "from":"In progress", + "to":"Completed", + "filterCond":"" + } ], - "revision": "3" + "revision":"3" }`, } }