diff --git a/handlers/aboutHandler.go b/handlers/aboutHandler.go new file mode 100644 index 0000000..90b07f9 --- /dev/null +++ b/handlers/aboutHandler.go @@ -0,0 +1,109 @@ +package handlers + +import ( + "encoding/json" + "fmt" + "net/http" + + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +/* + Copyright (C) 2023 Ulbora Labs LLC. (www.ulboralabs.com) + All rights reserved. + + Copyright (C) 2023 Ken Williamson + All rights reserved. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +// AddAbout AddAbout +func (h *MCHandler) AddAbout(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + aiOk := h.checkContent(r) + if !aiOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var ai db.About + ts, err := h.processBody(r, &ai) + h.Log.Debug("bs: ", ts) + h.Log.Debug("err: ", err) + if !ts || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + asuc, aid := h.DB.AddAbout(&ai) + h.Log.Debug("rsuc: ", asuc) + h.Log.Debug("rid: ", aid) + if asuc && aid != 0 { + w.WriteHeader(http.StatusOK) + var rres m.ResponseID + rres.ID = aid + rres.Success = asuc + resJSON, _ := json.Marshal(rres) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// UpdateAbout UpdateAbout +func (h *MCHandler) UpdateAbout(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + auucOk := h.checkContent(r) + if !auucOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var atu db.About + abs, err := h.processBody(r, &atu) + h.Log.Debug("bs: ", abs) + h.Log.Debug("err: ", err) + if !abs || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + asuc := h.DB.UpdateAbout(&atu) + h.Log.Debug("rsuc: ", asuc) + if asuc { + w.WriteHeader(http.StatusOK) + var tures m.Response + tures.Success = asuc + resJSON, _ := json.Marshal(tures) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// GetAbout GetAbout +func (h *MCHandler) GetAbout(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + var rtn *db.About + if !h.processAPIAdminKey(r) { + http.Error(w, "Bad Request", http.StatusBadRequest) + } else { + ags := h.DB.GetAbout() + if ags != nil && len(*ags) > 0 { + rtn = &(*ags)[0] + } else { + rtn = &db.About{} + } + w.WriteHeader(http.StatusOK) + resJSON, _ := json.Marshal(rtn) + fmt.Fprint(w, string(resJSON)) + } +} diff --git a/handlers/aboutHandler_test.go b/handlers/aboutHandler_test.go new file mode 100644 index 0000000..7a3daa2 --- /dev/null +++ b/handlers/aboutHandler_test.go @@ -0,0 +1,501 @@ +package handlers + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + lg "github.com/GolangToolKits/go-level-logger" + gdb "github.com/GolangToolKits/go-mysql" + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +func TestMCHandler_AddAbout(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockInsertSuccess1 = true + mdb.MockInsertID1 = 1 + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r2.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockInsertSuccess2 = false + mdb.MockInsertID2 = 0 + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.AddAbout(tt.args.w, tt.args.r) + + var res m.ResponseID + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_UpdateAbout(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockUpdateSuccess1 = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockUpdateSuccess2 = false + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.UpdateAbout(tt.args.w, tt.args.r) + + var res m.Response + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_GetAbout(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + + mdb.MockRows1 = &gdb.DbRows{ + Rows: [][]string{ + {"1", "test content entry"}, + {"2", "test content entry 333"}, + }, + } + mdb.MockConnectSuccess = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + r, _ := http.NewRequest("GET", "/ffllist", nil) + r.Header.Set("apiAdminKey", "1234") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("GET", "/ffllist", nil) + r2.Header.Set("apiAdminKey", "12342") + + w2 := httptest.NewRecorder() + + mdb.MockRows2 = &gdb.DbRows{ + Rows: [][]string{ + // {"1", "test content entry"}, + // {"2", "test content entry 333"}, + }, + } + r3, _ := http.NewRequest("GET", "/ffllist", nil) + r3.Header.Set("apiAdminKey", "1234") + + w3 := httptest.NewRecorder() + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + want string + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + want: "test content entry", + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w2, + r: r2, + }, + code: 400, + suc: false, + want: "", + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w3, + r: r3, + }, + code: 200, + suc: true, + want: "", + ww: w3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.GetAbout(tt.args.w, tt.args.r) + + var res db.Rule + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Content != tt.want { + t.Fail() + } + }) + } +} diff --git a/handlers/coverage.out b/handlers/coverage.out index da81908..9c3c3a3 100644 --- a/handlers/coverage.out +++ b/handlers/coverage.out @@ -1,4 +1,24 @@ mode: set +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:33.70,36.11 3 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:36.11,38.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:38.8,43.52 5 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:43.52,45.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:45.9,49.24 4 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:49.24,56.5 6 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:56.10,58.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:64.73,67.13 3 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:67.13,69.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:69.8,74.53 5 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:74.53,76.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:76.9,79.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:79.12,85.5 5 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:85.10,87.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:93.70,96.30 3 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:96.30,98.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:98.8,100.34 2 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:100.34,102.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:102.9,104.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/aboutHandler.go:105.3,107.33 3 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:35.69,38.11 3 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:38.11,40.3 1 1 github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:40.8,45.47 5 1 @@ -171,6 +191,46 @@ github.com/Ulbora/go-micro-blog/handlers/roleHandler.go:95.62,98.19 3 1 github.com/Ulbora/go-micro-blog/handlers/roleHandler.go:98.19,105.4 6 1 github.com/Ulbora/go-micro-blog/handlers/roleHandler.go:105.9,107.4 1 1 github.com/Ulbora/go-micro-blog/handlers/roleHandler.go:108.8,110.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:33.69,36.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:36.12,38.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:38.8,43.52 5 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:43.52,45.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:45.9,49.24 4 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:49.24,56.5 6 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:56.10,58.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:64.72,67.13 3 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:67.13,69.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:69.8,74.53 5 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:74.53,76.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:76.9,79.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:79.12,85.5 5 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:85.10,87.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:93.70,96.30 3 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:96.30,98.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:98.8,100.34 2 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:100.34,102.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:102.9,104.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/rulesHandler.go:105.3,107.33 3 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:33.70,36.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:36.12,38.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:38.8,43.52 5 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:43.52,45.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:45.9,49.24 4 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:49.24,56.5 6 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:56.10,58.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:64.73,67.13 3 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:67.13,69.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:69.8,74.53 5 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:74.53,76.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:76.9,79.12 3 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:79.12,85.5 5 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:85.10,87.5 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:93.70,96.30 3 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:96.30,98.3 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:98.8,100.34 2 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:100.34,102.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:102.9,104.4 1 1 +github.com/Ulbora/go-micro-blog/handlers/tosHandler.go:105.3,107.33 3 1 github.com/Ulbora/go-micro-blog/handlers/userAuthHandler.go:34.73,37.11 3 1 github.com/Ulbora/go-micro-blog/handlers/userAuthHandler.go:37.11,39.3 1 1 github.com/Ulbora/go-micro-blog/handlers/userAuthHandler.go:39.8,44.47 5 1 diff --git a/handlers/handler.go b/handlers/handler.go index 2eef958..5080c1e 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -72,14 +72,20 @@ type Handler interface { UpdateConfig(w http.ResponseWriter, r *http.Request) GetConfig(w http.ResponseWriter, r *http.Request) - //GetRules(w http.ResponseWriter, r *http.Request) - //SetRules(w http.ResponseWriter, r *http.Request) - - //GetTerms(w http.ResponseWriter, r *http.Request) - //SetTerms(w http.ResponseWriter, r *http.Request) - - //GetAbout(w http.ResponseWriter, r *http.Request) - //SetAbout((w http.ResponseWriter, r *http.Request)) + AddRule(w http.ResponseWriter, r *http.Request) + UpdateRule(w http.ResponseWriter, r *http.Request) + GetRules(w http.ResponseWriter, r *http.Request) + //=====SetRules(w http.ResponseWriter, r *http.Request) + + AddTerms(w http.ResponseWriter, r *http.Request) + UpdateTerms(w http.ResponseWriter, r *http.Request) + GetTerms(w http.ResponseWriter, r *http.Request) + //=====SetTerms(w http.ResponseWriter, r *http.Request) + + AddAbout(w http.ResponseWriter, r *http.Request) + UpdateAbout(w http.ResponseWriter, r *http.Request) + GetAbout(w http.ResponseWriter, r *http.Request) + //====SetAbout((w http.ResponseWriter, r *http.Request)) SetLogLevel(w http.ResponseWriter, r *http.Request) } diff --git a/handlers/rulesHandler.go b/handlers/rulesHandler.go new file mode 100644 index 0000000..3065f6e --- /dev/null +++ b/handlers/rulesHandler.go @@ -0,0 +1,109 @@ +package handlers + +import ( + "encoding/json" + "fmt" + "net/http" + + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +/* + Copyright (C) 2023 Ulbora Labs LLC. (www.ulboralabs.com) + All rights reserved. + + Copyright (C) 2023 Ken Williamson + All rights reserved. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +// AddRule AddRule +func (h *MCHandler) AddRule(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + rucOk := h.checkContent(r) + if !rucOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var ru db.Rule + bs, err := h.processBody(r, &ru) + h.Log.Debug("bs: ", bs) + h.Log.Debug("err: ", err) + if !bs || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + rsuc, rid := h.DB.AddRule(&ru) + h.Log.Debug("rsuc: ", rsuc) + h.Log.Debug("rid: ", rid) + if rsuc && rid != 0 { + w.WriteHeader(http.StatusOK) + var rres m.ResponseID + rres.ID = rid + rres.Success = rsuc + resJSON, _ := json.Marshal(rres) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// UpdateRule UpdateRule +func (h *MCHandler) UpdateRule(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + ruucOk := h.checkContent(r) + if !ruucOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var uru db.Rule + rbs, err := h.processBody(r, &uru) + h.Log.Debug("bs: ", rbs) + h.Log.Debug("err: ", err) + if !rbs || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + rsuc := h.DB.UpdateRule(&uru) + h.Log.Debug("rsuc: ", rsuc) + if rsuc { + w.WriteHeader(http.StatusOK) + var rures m.Response + rures.Success = rsuc + resJSON, _ := json.Marshal(rures) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// GetRules GetRules +func (h *MCHandler) GetRules(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + var rtn *db.Rule + if !h.processAPIAdminKey(r) { + http.Error(w, "Bad Request", http.StatusBadRequest) + } else { + cfg := h.DB.GetRule() + if cfg != nil && len(*cfg) > 0 { + rtn = &(*cfg)[0] + } else { + rtn = &db.Rule{} + } + w.WriteHeader(http.StatusOK) + resJSON, _ := json.Marshal(rtn) + fmt.Fprint(w, string(resJSON)) + } +} diff --git a/handlers/rulesHandler_test.go b/handlers/rulesHandler_test.go new file mode 100644 index 0000000..349ae51 --- /dev/null +++ b/handlers/rulesHandler_test.go @@ -0,0 +1,502 @@ +package handlers + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + lg "github.com/GolangToolKits/go-level-logger" + gdb "github.com/GolangToolKits/go-mysql" + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +func TestMCHandler_AddRule(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockInsertSuccess1 = true + mdb.MockInsertID1 = 1 + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r2.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockInsertSuccess2 = false + mdb.MockInsertID2 = 0 + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.AddRule(tt.args.w, tt.args.r) + + var res m.ResponseID + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_UpdateRule(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockUpdateSuccess1 = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockUpdateSuccess2 = false + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.UpdateRule(tt.args.w, tt.args.r) + + var res m.Response + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_GetRules(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + + mdb.MockRows1 = &gdb.DbRows{ + Rows: [][]string{ + {"1", "test content entry"}, + {"2", "test content entry 333"}, + }, + } + mdb.MockConnectSuccess = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + r, _ := http.NewRequest("GET", "/ffllist", nil) + r.Header.Set("apiAdminKey", "1234") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("GET", "/ffllist", nil) + r2.Header.Set("apiAdminKey", "12342") + + w2 := httptest.NewRecorder() + + + mdb.MockRows2 = &gdb.DbRows{ + Rows: [][]string{ + // {"1", "test content entry"}, + // {"2", "test content entry 333"}, + }, + } + r3, _ := http.NewRequest("GET", "/ffllist", nil) + r3.Header.Set("apiAdminKey", "1234") + + w3 := httptest.NewRecorder() + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + want string + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + want: "test content entry", + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w2, + r: r2, + }, + code: 400, + suc: false, + want: "", + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w3, + r: r3, + }, + code: 200, + suc: true, + want: "", + ww: w3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.GetRules(tt.args.w, tt.args.r) + + var res db.Rule + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Content != tt.want { + t.Fail() + } + }) + } +} diff --git a/handlers/tosHandler.go b/handlers/tosHandler.go new file mode 100644 index 0000000..22dfcc6 --- /dev/null +++ b/handlers/tosHandler.go @@ -0,0 +1,109 @@ +package handlers + +import ( + "encoding/json" + "fmt" + "net/http" + + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +/* + Copyright (C) 2023 Ulbora Labs LLC. (www.ulboralabs.com) + All rights reserved. + + Copyright (C) 2023 Ken Williamson + All rights reserved. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +// AddTerms AddTerms +func (h *MCHandler) AddTerms(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + tucOk := h.checkContent(r) + if !tucOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var ti db.Tos + ts, err := h.processBody(r, &ti) + h.Log.Debug("bs: ", ts) + h.Log.Debug("err: ", err) + if !ts || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + tsuc, tid := h.DB.AddTos(&ti) + h.Log.Debug("rsuc: ", tsuc) + h.Log.Debug("rid: ", tid) + if tsuc && tid != 0 { + w.WriteHeader(http.StatusOK) + var rres m.ResponseID + rres.ID = tid + rres.Success = tsuc + resJSON, _ := json.Marshal(rres) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// UpdateTerms UpdateTerms +func (h *MCHandler) UpdateTerms(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + tuucOk := h.checkContent(r) + if !tuucOk { + http.Error(w, "json required", http.StatusUnsupportedMediaType) + } else { + var utu db.Tos + rbs, err := h.processBody(r, &utu) + h.Log.Debug("bs: ", rbs) + h.Log.Debug("err: ", err) + if !rbs || err != nil || !h.processAPIAdminKey(r) { + http.Error(w, parseBodyErr, http.StatusBadRequest) + } else { + tsuc := h.DB.UpdateTos(&utu) + h.Log.Debug("rsuc: ", tsuc) + if tsuc { + w.WriteHeader(http.StatusOK) + var tures m.Response + tures.Success = tsuc + resJSON, _ := json.Marshal(tures) + fmt.Fprint(w, string(resJSON)) + } else { + w.WriteHeader(http.StatusInternalServerError) + } + } + } +} + +// GetTerms GetTerms +func (h *MCHandler) GetTerms(w http.ResponseWriter, r *http.Request) { + h.setContentType(w) + var rtn *db.Tos + if !h.processAPIAdminKey(r) { + http.Error(w, "Bad Request", http.StatusBadRequest) + } else { + tgs := h.DB.GetTos() + if tgs != nil && len(*tgs) > 0 { + rtn = &(*tgs)[0] + } else { + rtn = &db.Tos{} + } + w.WriteHeader(http.StatusOK) + resJSON, _ := json.Marshal(rtn) + fmt.Fprint(w, string(resJSON)) + } +} diff --git a/handlers/tosHandler_test.go b/handlers/tosHandler_test.go new file mode 100644 index 0000000..1e19611 --- /dev/null +++ b/handlers/tosHandler_test.go @@ -0,0 +1,501 @@ +package handlers + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + lg "github.com/GolangToolKits/go-level-logger" + gdb "github.com/GolangToolKits/go-mysql" + db "github.com/Ulbora/go-micro-blog/db" + m "github.com/Ulbora/go-micro-blog/managers" +) + +func TestMCHandler_AddTos(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockInsertSuccess1 = true + mdb.MockInsertID1 = 1 + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r2.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"content": "stuff"}`)) + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockInsertSuccess2 = false + mdb.MockInsertID2 = 0 + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.AddTerms(tt.args.w, tt.args.r) + + var res m.ResponseID + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_UpdateTos(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + mdb.MockConnectSuccess = true + mdb.MockUpdateSuccess1 = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + aJSON := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r, _ := http.NewRequest("POST", "/ffllist", aJSON) + r.Header.Set("apiAdminKey", "1234") + r.Header.Set("Content-Type", "application/json") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("POST", "/ffllist", aJSON) + r2.Header.Set("apiAdminKey", "1234") + //r.Header.Set("Content-Type", "application/json") + + w2 := httptest.NewRecorder() + + r3, _ := http.NewRequest("POST", "/ffllist", aJSON) + r3.Header.Set("apiAdminKey", "12343") + r3.Header.Set("Content-Type", "application/json") + + w3 := httptest.NewRecorder() + + aJSON4 := ioutil.NopCloser(bytes.NewBufferString(`{"id": 5, "content": "stuff"}`)) + + r4, _ := http.NewRequest("POST", "/ffllist", aJSON4) + r4.Header.Set("apiAdminKey", "1234") + r4.Header.Set("Content-Type", "application/json") + + w4 := httptest.NewRecorder() + + mdb.MockUpdateSuccess2 = false + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + len int + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + len: 0, + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w2, + r: r2, + }, + code: 415, + suc: false, + len: 0, + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w3, + r: r3, + }, + code: 400, + suc: false, + len: 0, + ww: w3, + }, + { + name: "test 4", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + }, + args: args{ + w: w4, + r: r4, + }, + code: 500, + suc: false, + len: 0, + ww: w4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.UpdateTerms(tt.args.w, tt.args.r) + + var res m.Response + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Success != tt.suc { + t.Fail() + } + }) + } +} + +func TestMCHandler_GetTos(t *testing.T) { + + mdb := gdb.MyDBMock{ + Host: "localhost:3306", + User: "admin", + Password: "admin", + Database: "go_micro_blog", + } + mdb.MockTestRow = &gdb.DbRow{ + //Row: []string{"0"}, + Row: []string{}, + } + + mdb.MockRows1 = &gdb.DbRows{ + Rows: [][]string{ + {"1", "test content entry"}, + {"2", "test content entry 333"}, + }, + } + mdb.MockConnectSuccess = true + + var l lg.Logger + log := l.New() + log.SetLogLevel(lg.AllLevel) + + r, _ := http.NewRequest("GET", "/ffllist", nil) + r.Header.Set("apiAdminKey", "1234") + + w := httptest.NewRecorder() + + r2, _ := http.NewRequest("GET", "/ffllist", nil) + r2.Header.Set("apiAdminKey", "12342") + + w2 := httptest.NewRecorder() + + mdb.MockRows2 = &gdb.DbRows{ + Rows: [][]string{ + // {"1", "test content entry"}, + // {"2", "test content entry 333"}, + }, + } + r3, _ := http.NewRequest("GET", "/ffllist", nil) + r3.Header.Set("apiAdminKey", "1234") + + w3 := httptest.NewRecorder() + + type fields struct { + DB db.BlogDB + Log lg.Log + Manager m.Manager + APIKey string + APIAdminKey string + } + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + fields fields + args args + code int + suc bool + want string + ww *httptest.ResponseRecorder + }{ + // TODO: Add test cases. + { + name: "test 1", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w, + r: r, + }, + code: 200, + suc: true, + want: "test content entry", + ww: w, + }, + { + name: "test 2", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w2, + r: r2, + }, + code: 400, + suc: false, + want: "", + ww: w2, + }, + { + name: "test 3", + fields: fields{ + DB: &db.MyBlogDB{ + DB: &mdb, + Log: log, + }, + Log: log, + APIAdminKey: "1234", + //Manager: mg.New(), + }, + args: args{ + w: w3, + r: r3, + }, + code: 200, + suc: true, + want: "", + ww: w3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &MCHandler{ + DB: tt.fields.DB, + Log: tt.fields.Log, + Manager: tt.fields.Manager, + APIKey: tt.fields.APIKey, + APIAdminKey: tt.fields.APIAdminKey, + } + h.GetTerms(tt.args.w, tt.args.r) + + var res db.Rule + body, _ := ioutil.ReadAll(w.Result().Body) + json.Unmarshal(body, &res) + if tt.ww.Code != tt.code || res.Content != tt.want { + t.Fail() + } + }) + } +} diff --git a/server b/server index f9e8677..1e2b3de 100755 Binary files a/server and b/server differ diff --git a/server.go b/server.go index 2bff9dc..795c9de 100644 --- a/server.go +++ b/server.go @@ -171,6 +171,18 @@ func main() { router.HandleFunc("/rs/config/update", h.UpdateConfig).Methods("PUT") router.HandleFunc("/rs/config/get", h.GetConfig).Methods("GET") + router.HandleFunc("/rs/rule/add", h.AddRule).Methods("POST") + router.HandleFunc("/rs/rule/update", h.UpdateRule).Methods("PUT") + router.HandleFunc("/rs/rule/get", h.GetRules).Methods("GET") + + router.HandleFunc("/rs/tos/add", h.AddTerms).Methods("POST") + router.HandleFunc("/rs/tos/update", h.UpdateTerms).Methods("PUT") + router.HandleFunc("/rs/tos/get", h.GetTerms).Methods("GET") + + router.HandleFunc("/rs/about/add", h.AddAbout).Methods("POST") + router.HandleFunc("/rs/about/update", h.UpdateAbout).Methods("PUT") + router.HandleFunc("/rs/about/get", h.GetAbout).Methods("GET") + //logger router.HandleFunc("/rs/loglevel", h.SetLogLevel).Methods("POST")