Skip to content

Commit

Permalink
re-arrange program structure
Browse files Browse the repository at this point in the history
- re-arrange program structure to make testing easier on App structure
- presenting methods to reduce busy writing
  • Loading branch information
zkrhm committed Sep 2, 2018
1 parent 5a07cd6 commit c1139d0
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 150 deletions.
3 changes: 2 additions & 1 deletion app.go → app/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"fmt"
Expand Down Expand Up @@ -69,6 +69,7 @@ func (app *App) initRoutes() {
router.HandleFunc("/friend-list", app.GetFriendList)
router.HandleFunc("/common-friends", app.GetCommonFriends)
router.HandleFunc("/subscribe", app.Subscribe)
router.HandleFunc("/list-subscriber", app.ListSubscribers)
router.HandleFunc("/block", app.Block)
router.HandleFunc("/post-update", app.PostUpdate)
router.Use(jsonMiddleware)
Expand Down
18 changes: 6 additions & 12 deletions handler.go → app/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main
package app

import (
"io/ioutil"
"fmt"
"net/http"

Expand Down Expand Up @@ -52,9 +51,8 @@ func (app *App) ConnectAsFriend(res http.ResponseWriter, req *http.Request) {

return
}

helper.WriteReponse(res, ConnectFriendResponse{Success: true})

}

func (app *App) GetFriendList(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -158,7 +156,7 @@ func (app *App) Subscribe(w http.ResponseWriter, r *http.Request) {
})
}

func (app *App) Subscriber(w http.ResponseWriter, r *http.Request){
func (app *App) ListSubscribers(w http.ResponseWriter, r *http.Request){
reqObj := &SubscriberListRequest{}
err := helper.GetRequest(r, reqObj)

Expand Down Expand Up @@ -202,10 +200,6 @@ func (app *App) Block(w http.ResponseWriter, r *http.Request) {
reqObj := &BlockRequest{}
err := helper.GetRequest(r, reqObj)

b, err := ioutil.ReadAll(r.Body)
fmt.Println("ioutil err :",err)
fmt.Println("request body: ",string(b))
defer r.Body.Close()
if err != nil {
helper.WriteReponse(w, BlockResponse{
Code : http.StatusUnprocessableEntity,
Expand All @@ -215,8 +209,6 @@ func (app *App) Block(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Println("request object :", reqObj.Requestor, ".target:",reqObj.Target)
// check format
err1 := checkmail.ValidateFormat(reqObj.Requestor)
err2 := checkmail.ValidateFormat(reqObj.Target)

Expand Down Expand Up @@ -255,16 +247,18 @@ func (app *App) PostUpdate(w http.ResponseWriter, r *http.Request) {
Message: "Invalid email format",
Success: false,
})
return
}

recipients, err := app.DB.DoUpdate(User(reqObj.Sender), reqObj.Text)

if err != nil {
helper.WriteReponse(w, UpdateResponse{
Code: http.StatusInternalServerError,
Code: err.(*errors.ErrorWithCode).Code(),
Message: err.Error(),
Success: false,
})
return
}

helper.WriteReponse(w, UpdateResponse{
Expand Down
177 changes: 129 additions & 48 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,75 @@
package main_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"

. "github.com/zkrhm/imd-socialnetwork"
"github.com/zkrhm/imd-socialnetwork/app"
. "github.com/zkrhm/imd-socialnetwork/model"
"github.com/zkrhm/imd-socialnetwork/db"
. "github.com/zkrhm/imd-socialnetwork/testhelper"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
// . "github.com/Benjamintf1/ExpandedUnmarshalledMatchers"
)

const(
bob = "[email protected]"
alice = "[email protected]"
greg = "[email protected]"
fred = "[email protected]"
emily = "[email protected]"
dani = "[email protected]"
charlie = "[email protected]"
jonathan = "[email protected]"
maria = "[email protected]"
notUser = "[email protected]"
notEmail = "not-email-addr"
)



var _ = Describe("Friend Management Specs", func() {

Describe("HELPER method - testing helper endpoints", func(){

app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()

Context("GET SUBSCRIBERS - ", func(){
It("returns all of subscribers", func(){
By("using [email protected] as parameter")

const reqBody = `{
"email": "[email protected]"
}`
test := NewHttpTest("POST","/subscribers",reqBody,app.ListSubscribers)
rr , err := test.DoRequestTest()

Expect(err).ShouldNot(HaveOccurred())
Expect(rr.Code).Should(Equal(200))
resObj := &SubscriberListResponse{}
json.Unmarshal([]byte(rr.Body.String()),resObj)
Expect(resObj.Subscribers).Should(
ConsistOf([]string{
alice,
charlie,
dani,
fred,
}),
)

})
})
})
Describe("R1 - As a user I need to create friend connection between two email addresses", func() {

app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
Expand Down Expand Up @@ -85,12 +137,6 @@ var _ = Describe("Friend Management Specs", func() {
})
})

PContext("Connecting already connected two user", func() {
It("Complains that users already friends", func() {

})
})

Context("Connecting non-existent user", func() {
It("Complains that non-existent user are not available on the system", func() {
rr := httptest.NewRecorder()
Expand All @@ -114,29 +160,31 @@ var _ = Describe("Friend Management Specs", func() {
})
})

PContext("Connecting blocked users", func() {
Context("Connecting blocked users", func() {

It("Throws error that the user cannot be connected because of blockage", func() {

rr := httptest.NewRecorder()
const reqBody = `{
"friends": ["[email protected]","[email protected]"]
}`
req, err := http.NewRequest("POST", "/connect", strings.NewReader(reqBody))
By("user maria who have not been bob's user doing block")

res, err := DoBlock(app, maria, bob)
Expect(err).ShouldNot(HaveOccurred())
handler.ServeHTTP(rr, req)
Expect(res.Success).Should(BeTrue())

const expectedResponse = `{
By("checking status of doing connect, should be false ")

res2, err := DoConnect(app, bob, maria)
// fmt.Println(res2.Message)
Expect(res2.Code).Should(Equal(http.StatusForbidden))

Expect(res2.Success).Should(BeFalse())

}`
Expect(rr.Code).To(Equal(http.StatusForbidden))
})
})
})

Describe("R2 - I need to retrieve friend list of an email user", func() {

app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
Expand Down Expand Up @@ -211,7 +259,7 @@ var _ = Describe("Friend Management Specs", func() {
})

Describe("R3 - I need to retrieve common friends between two email address", func() {
app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
Expand Down Expand Up @@ -291,7 +339,7 @@ var _ = Describe("Friend Management Specs", func() {

Describe("R4 - I need API to subscribe to updates from email address", func() {

app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
Expand Down Expand Up @@ -360,7 +408,7 @@ var _ = Describe("Friend Management Specs", func() {

Describe("R5 - I need an API to block updates from an email address", func() {

app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
Expand All @@ -375,7 +423,7 @@ var _ = Describe("Friend Management Specs", func() {
Expect(err).ShouldNot(HaveOccurred())
handler.ServeHTTP(rr, req)

fmt.Println("http response:",rr.Body.String())
// fmt.Println("http response:",rr.Body.String())

Expect(err).ShouldNot(HaveOccurred())
Expect(rr.Code).To(Equal(200))
Expand All @@ -388,7 +436,7 @@ var _ = Describe("Friend Management Specs", func() {
Expect(err).ShouldNot(HaveOccurred())
handler.ServeHTTP(rr, req)

fmt.Println("http response:",rr.Body.String())
// fmt.Println("http response:",rr.Body.String())

Expect(err).ShouldNot(HaveOccurred())
Expect(rr.Code).To(Equal(200))
Expand All @@ -398,53 +446,86 @@ var _ = Describe("Friend Management Specs", func() {

})



Describe("R6 - I need an API to retrieve all email address that can receive update from an email address", func() {
app := NewApp()
app := app.NewApp()
store, _ := db.NewCayleyStore()
app.UseDb(store)
app.Initialize()
handler := http.HandlerFunc(app.PostUpdate)

Context("Normal Flow", func() {
It("returns list of subscrbers", func() {
By("blocking non-friend user")
rr := httptest.NewRecorder()
const reqBody = `{
"sender":"[email protected]",
"text":"Hello Folks!"
}`
req, err := http.NewRequest("POST", "/post-update", strings.NewReader(reqBody))
Expect(err).ShouldNot(HaveOccurred())
handler.ServeHTTP(rr, req)

fmt.Println("http response:",rr.Body.String())

Expect(err).ShouldNot(HaveOccurred())
Expect(rr.Code).To(Equal(200))
It("list all friend as recipient if no one mentioned in the message", func() {
By(fmt.Sprint("sending messages from",bob," without any mentions"))

res := PostUpdating(app,bob,"Hello Folks")
Expect(res.Recipients).Should(ConsistOf([]string{alice,charlie,dani,fred}))
})

PIt("Includes not-blocked user in the result", func() {
It("Exclude friend who blocking user who the update in the result", func() {

By("user alice who is bob's friend block bob")

res , _ := DoBlock(app, alice, bob)
// Expect(err).ShouldNot(HaveOccurred())
Expect(res.Success).Should(BeTrue())


By("checking who is included as recipients, alice should not be included")

res2 := PostUpdating(app,bob,"Hello Everyone")

Expect(res2.Recipients).ShouldNot(ContainElement(alice))

})

PIt("Includes subscribed user in the result", func() {
It("Includes subscribed user who is not a friend", func() {

By("user maria who is not a friend of bob doing subscribe")
subReq , err := (&SubscribeRequest{
Requestor : maria,
Target: bob,
}).Marshal()
Expect(err).ShouldNot(HaveOccurred())

subReqObj := NewHttpTest("POST","/subscribe",string(subReq),app.Subscribe)
rr, err := subReqObj.DoRequestTest()
Expect(err).ShouldNot(HaveOccurred())
Expect(rr.Code).Should(Equal(http.StatusOK))

By("user bob post an update without mentioning anyone, maria should be listed")
res := PostUpdating(app, bob, "Hello Everyone!")

Expect(res.Recipients).Should(ContainElement(maria))

})

PIt("Includes user who is mentioned in an update", func() {
It("Includes registered user who is mentioned in an update, even not a friend", func() {
res := PostUpdating(app, bob, fmt.Sprint("How are you ",charlie,". long time no see!"))
Expect(res.Recipients).Should(ContainElement(charlie))
})

It("Excludes non-registerd user who is mentioned in an update", func(){
res := PostUpdating(app, bob, fmt.Sprint("How are you ",notUser,". long time no see!"))
Expect(res.Recipients).ShouldNot(ContainElement(notUser))
})


})

PContext("Alternate Flow - user with no subscribers", func() {
Context("Alternate Flow - user with no subscribers", func() {
It("returns empty set of subscriber", func() {

res := PostUpdating(app, maria, "Hello No one")
Expect(res.Recipients).Should(BeEmpty())
})
})

PContext("Alternate FLow - requesting subscribers of non existing email address", func() {
Context("Alternate FLow - requesting subscribers of non existing email address", func() {
It("Throws error that the requested email address is non existent", func() {

res := PostUpdating(app, notUser, "Hello Not a user")
Expect(res.Success).To(BeFalse())
Expect(res.Code).To(Equal(http.StatusNotFound))
})
})
})
Expand Down
Loading

0 comments on commit c1139d0

Please sign in to comment.