Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
KenWilliamson committed Aug 31, 2023
1 parent ac4dde2 commit 6f01064
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 35 deletions.
27 changes: 27 additions & 0 deletions handlers/blogHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ func (h *MCHandler) GetBlogByName(w http.ResponseWriter, r *http.Request) {
}
}

// GetAdminBlogByName GetAdminBlogByName
func (h *MCHandler) GetAdminBlogByName(w http.ResponseWriter, r *http.Request) {
h.setContentType(w)
avars := mux.Vars(r)
h.Log.Debug("vars: ", len(avars))
if avars != nil && len(avars) == 3 && h.processAPIAdminKey(r) {
var name = avars["name"]
h.Log.Debug("name: ", name)
var astStr = avars["start"]
var aedStr = avars["end"]
st, sterr := strconv.ParseInt(astStr, 10, 64)
ed, ederr := strconv.ParseInt(aedStr, 10, 64)

if sterr == nil && ederr == nil {
//blg := h.Manager.GetBlogByName(name, st, ed)
blg := h.DB.GetBlogsByName(name, st, ed)
w.WriteHeader(http.StatusOK)
resJSON, _ := json.Marshal(blg)
fmt.Fprint(w, string(resJSON))
} else {
w.WriteHeader(http.StatusBadRequest)
}
} else {
w.WriteHeader(http.StatusBadRequest)
}
}

// GetBlogList GetBlogList
func (h *MCHandler) GetBlogList(w http.ResponseWriter, r *http.Request) {
h.setContentType(w)
Expand Down
177 changes: 173 additions & 4 deletions handlers/blogHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,177 @@ func TestMCHandler_GetBlogByName(t *testing.T) {
}
}

func TestMCHandler_GetAdminBlogByName(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 blog entry", "some test blog stuff", "4", "true", "2023-03-01 00:01:14", ""},
{"2", "test blog entry 333", "some test blog stuff", "4", "false", "2023-03-01 00:01:14", "2023-03-01 00:01:14"}},
}

var l lg.Logger
log := l.New()
log.SetLogLevel(lg.AllLevel)

r, _ := http.NewRequest("GET", "/ffllist", nil)

vars := map[string]string{
"name": "test",
"start": "1",
"end": "5",
}
r = mux.SetURLVars(r, vars)
r.Header.Set("apiAdminKey", "1234")
//r.Header.Set("apiKey", "1234")
//r.Header.Set("Content-Type", "application/json")

w := httptest.NewRecorder()




r2, _ := http.NewRequest("GET", "/ffllist", nil)

vars2 := map[string]string{
"name": "test",
"start": "1",
"end": "5",
}
r2 = mux.SetURLVars(r2, vars2)
r2.Header.Set("apiAdminKey", "12346")
//r.Header.Set("apiKey", "1234")
//r.Header.Set("Content-Type", "application/json")

w2 := httptest.NewRecorder()



r3, _ := http.NewRequest("GET", "/ffllist", nil)

vars3 := map[string]string{
"name": "test",
"start": "1u",
"end": "5",
}
r3 = mux.SetURLVars(r3, vars3)
r3.Header.Set("apiAdminKey", "1234")
//r.Header.Set("apiKey", "1234")
//r.Header.Set("Content-Type", "application/json")

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
len int
ww *httptest.ResponseRecorder
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
DB: &db.MyBlogDB{
DB: &mdb,
Log: log,
},
Log: log,
// Manager: mg.New(),
APIAdminKey: "1234",
},
args: args{
w: w,
r: r,
},
code: 200,
suc: true,
len: 2,
ww: w,
},
{
name: "test 2",
fields: fields{
DB: &db.MyBlogDB{
DB: &mdb,
Log: log,
},
Log: log,
// Manager: mg.New(),
APIAdminKey: "1234",
},
args: args{
w: w2,
r: r2,
},
code: 400,
suc: false,
len: 0,
ww: w2,
},
{
name: "test 3",
fields: fields{
DB: &db.MyBlogDB{
DB: &mdb,
Log: log,
},
Log: log,
// Manager: mg.New(),
APIAdminKey: "1234",
},
args: args{
w: w3,
r: r3,
},
code: 400,
suc: true,
len: 0,
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.GetAdminBlogByName(tt.args.w, tt.args.r)
var res []db.Blog
body, _ := ioutil.ReadAll(w.Result().Body)
json.Unmarshal(body, &res)
if tt.ww.Code != tt.code || len(res) != tt.len {
t.Fail()
}
})
}
}

func TestMCHandler_GetBlogList(t *testing.T) {

var l lg.Logger
Expand Down Expand Up @@ -1395,7 +1566,6 @@ func TestMCHandler_DeleteBlog(t *testing.T) {

w3 := httptest.NewRecorder()


r4, _ := http.NewRequest("GET", "/ffllist", nil)
r4.Header.Set("apiAdminKey", "1234")
vars4 := map[string]string{
Expand All @@ -1407,7 +1577,6 @@ func TestMCHandler_DeleteBlog(t *testing.T) {

mdb.MockDeleteSuccess2 = false


//mdb.MockDeleteSuccess1 = false

type fields struct {
Expand Down Expand Up @@ -1495,7 +1664,7 @@ func TestMCHandler_DeleteBlog(t *testing.T) {
DB: &mdb,
Log: log,
},
Log: log,
Log: log,
APIAdminKey: "1234",
},
args: args{
Expand All @@ -1504,7 +1673,7 @@ func TestMCHandler_DeleteBlog(t *testing.T) {
},
code: 500,
suc: false,
len: 0,
len: 0,
ww: w4,
},
}
Expand Down
67 changes: 36 additions & 31 deletions handlers/coverage.out
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,42 @@ github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:118.35,126.35 7 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:126.35,131.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:131.9,133.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:134.8,136.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:140.73,144.35 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:144.35,150.35 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:150.35,155.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:155.9,157.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:158.8,160.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:164.78,168.62 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:168.62,174.35 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:174.35,179.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:179.9,181.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:182.8,184.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:188.74,191.11 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:191.11,193.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:193.8,198.52 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:198.52,200.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:200.9,205.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:205.19,209.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:209.10,211.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:217.75,220.12 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:220.12,222.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:222.8,227.53 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:227.53,229.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:229.9,234.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:234.19,238.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:238.10,240.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:246.72,250.62 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:250.62,254.19 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:254.19,259.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:259.19,263.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:263.10,265.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:266.9,268.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:269.8,271.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:140.80,144.64 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:144.64,152.35 7 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:152.35,158.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:158.9,160.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:161.8,163.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:167.73,171.35 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:171.35,177.35 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:177.35,182.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:182.9,184.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:185.8,187.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:191.78,195.62 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:195.62,201.35 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:201.35,206.4 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:206.9,208.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:209.8,211.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:215.74,218.11 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:218.11,220.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:220.8,225.52 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:225.52,227.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:227.9,232.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:232.19,236.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:236.10,238.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:244.75,247.12 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:247.12,249.3 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:249.8,254.53 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:254.53,256.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:256.9,261.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:261.19,265.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:265.10,267.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:273.72,277.62 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:277.62,281.19 4 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:281.19,286.19 5 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:286.19,290.5 3 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:290.10,292.5 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:293.9,295.4 1 1
github.com/Ulbora/go-micro-blog/handlers/blogHandler.go:296.8,298.3 1 1
github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:35.72,38.11 3 1
github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:38.11,40.3 1 1
github.com/Ulbora/go-micro-blog/handlers/commentHandler.go:40.8,45.47 5 1
Expand Down
1 change: 1 addition & 0 deletions handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Handler interface {
UpdateBlog(w http.ResponseWriter, r *http.Request)
GetBlog(w http.ResponseWriter, r *http.Request)
GetBlogByName(w http.ResponseWriter, r *http.Request)
GetAdminBlogByName(w http.ResponseWriter, r *http.Request)
GetBlogList(w http.ResponseWriter, r *http.Request)
GetAdminBlogList(w http.ResponseWriter, r *http.Request)
ActivateBlog(w http.ResponseWriter, r *http.Request)
Expand Down
Binary file modified server
Binary file not shown.
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func main() {
router.HandleFunc("/rs/blog/update", h.UpdateBlog).Methods("PUT")
router.HandleFunc("/rs/blog/get/id/{id}", h.GetBlog).Methods("GET")
router.HandleFunc("/rs/blog/get/name/{name}/{start}/{end}", h.GetBlogByName).Methods("GET")
router.HandleFunc("/rs/blog/admin/get/name/{name}/{start}/{end}", h.GetAdminBlogByName).Methods("GET")
router.HandleFunc("/rs/blog/list/{start}/{end}", h.GetBlogList).Methods("GET")
router.HandleFunc("/rs/blog/admin/list/{start}/{end}", h.GetAdminBlogList).Methods("GET")
router.HandleFunc("/rs/blog/activate", h.ActivateBlog).Methods("PUT")
Expand Down

0 comments on commit 6f01064

Please sign in to comment.