Skip to content

Commit

Permalink
Add plurk add/response test to robot
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Aug 6, 2015
1 parent ba2630b commit 51e5600
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
156 changes: 156 additions & 0 deletions robot/mod_plurk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package robot

import (
"github.com/ddliu/motto"
"github.com/elct9620/go-plurk-robot/plurk"
"github.com/robertkrimen/otto"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"text/template"
)

func buildPlurkAddServer(responseFile string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
id := 999999
content := r.PostFormValue("content")
lang := r.PostFormValue("lang")
qualifier := r.PostFormValue("qualifier")

resData := &plurk.Plurk{
PlurkID: id,
RawContent: content,
Language: lang,
Qualifier: qualifier,
}

ts, _ := template.ParseFiles(responseFile)
ts.Execute(w, resData)
}))
}

func buildResponseAddServer(responseFile string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
idString := r.PostFormValue("plurk_id")
content := r.PostFormValue("content")
qualifier := r.PostFormValue("qualifier")

id, _ := strconv.Atoi(idString)

resData := &plurk.Plurk{
PlurkID: id,
RawContent: content,
Language: "en",
Qualifier: qualifier,
}

ts, _ := template.ParseFiles(responseFile)
ts.Execute(w, resData)
}))
}

func Test_plurkModuleLoader(t *testing.T) {
vm := motto.New()
module, _ := plurkModuleLoader(vm)
// Convert to Object
moduleObject := module.Object()
keys := moduleObject.Keys()

assert.Contains(t, keys, "addPlurk")
assert.Contains(t, keys, "addResponse")
}

func TestAddPlurk(t *testing.T) {
testPath := "test/plurk_add.json"
server := buildPlurkAddServer(testPath)
defer server.Close()

client = &plurk.PlurkClient{ApiBase: server.URL}

_, content, _ := otto.Run(`("Hello World")`)
funcCall := otto.FunctionCall{}
funcCall.ArgumentList = append(funcCall.ArgumentList, content)

result := plurk_AddPlurk(funcCall)
success, _ := result.ToBoolean()

assert.True(t, success)
}

func TestAddPlurkNoContent(t *testing.T) {
testPath := "test/plurk_add.json"
server := buildPlurkAddServer(testPath)
defer server.Close()

client = &plurk.PlurkClient{ApiBase: server.URL}

_, content, _ := otto.Run(`("")`)
funcCall := otto.FunctionCall{}
funcCall.ArgumentList = append(funcCall.ArgumentList, content)

result := plurk_AddPlurk(funcCall)
success, _ := result.ToBoolean()

assert.False(t, success)
}

func TestAddResponse(t *testing.T) {
testPath := "test/response_add.json"
server := buildResponseAddServer(testPath)
defer server.Close()

client = &plurk.PlurkClient{ApiBase: server.URL}

_, plurkID, _ := otto.Run(`(123456)`)
_, content, _ := otto.Run(`("Hello World")`)
funcCall := otto.FunctionCall{}
funcCall.ArgumentList = append(funcCall.ArgumentList, plurkID)
funcCall.ArgumentList = append(funcCall.ArgumentList, content)

result := plurk_AddResponse(funcCall)
success, _ := result.ToBoolean()

assert.True(t, success)
}

func TestAddResponseInvalidID(t *testing.T) {
testPath := "test/response_add.json"
server := buildResponseAddServer(testPath)
defer server.Close()

client = &plurk.PlurkClient{ApiBase: server.URL}

_, plurkID, _ := otto.Run(`("Invalid")`)
_, content, _ := otto.Run(`("Hello World")`)
funcCall := otto.FunctionCall{}
funcCall.ArgumentList = append(funcCall.ArgumentList, plurkID)
funcCall.ArgumentList = append(funcCall.ArgumentList, content)

result := plurk_AddResponse(funcCall)
success, _ := result.ToBoolean()

assert.False(t, success)
}

func TestAddResponseNoContent(t *testing.T) {
testPath := "test/response_add.json"
server := buildResponseAddServer(testPath)
defer server.Close()

client = &plurk.PlurkClient{ApiBase: server.URL}

_, plurkID, _ := otto.Run(`(123456)`)
funcCall := otto.FunctionCall{}
funcCall.ArgumentList = append(funcCall.ArgumentList, plurkID)

result := plurk_AddResponse(funcCall)
success, _ := result.ToBoolean()

assert.False(t, success)
}
8 changes: 8 additions & 0 deletions robot/robot_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package robot

import (
"github.com/elct9620/go-plurk-robot/plurk"
"github.com/stretchr/testify/assert"
"syscall"
"testing"
)

func TestSetupPlurk(t *testing.T) {
clientExpect := plurk.New("A", "B", "C", "D")
SetupPlurk("A", "B", "C", "D")

assert.Equal(t, clientExpect, client)
}

func TestNew(t *testing.T) {
robot := New()

Expand Down
28 changes: 28 additions & 0 deletions robot/test/plurk_add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"twitter_disabled": true,
"replurkers": [],
"responses_seen": 0,
"qualifier": "{{.Qualifier}}",
"replurkers_count": 0,
"plurk_id": {{.PlurkID}},
"response_count": 0,
"anonymous": false,
"replurkable": false,
"limited_to": "",
"favorite_count": 0,
"is_unread": 0,
"lang": "{{.Language}}",
"favorers": [],
"content_raw": "{{.RawContent}}",
"user_id": 999999,
"plurk_type": 1,
"qualifier_translated": "",
"replurked": false,
"favorite": false,
"no_comments": 0,
"content": "{{.RawContent}}",
"replurker_id": null,
"facebook_disabled": true,
"posted": "Thu, 16 Jul 2015 15:27:00 GMT",
"owner_id": 999999
}
10 changes: 10 additions & 0 deletions robot/test/response_add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"lang": "{{.Language}}",
"content_raw": "{{.RawContent}}",
"user_id": 999999,
"qualifier": "{{.Qualifier}}",
"plurk_id": {{.PlurkID}},
"content": "{{.Content}}",
"id": 6310533052,
"posted": "Fri, 17 Jul 2015 04:08:56 GMT"
}

0 comments on commit 51e5600

Please sign in to comment.