From 52ee17dae8dd607065122ffb150997659a8a8577 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 11 Oct 2017 05:49:41 -0400 Subject: [PATCH] testing: Add session deletion test --- internal/handlers/securities_test.go | 2 +- internal/handlers/sessions_test.go | 42 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/internal/handlers/securities_test.go b/internal/handlers/securities_test.go index aecdb6f..f749b61 100644 --- a/internal/handlers/securities_test.go +++ b/internal/handlers/securities_test.go @@ -162,7 +162,7 @@ func TestDeleteSecurity(t *testing.T) { t.Fatalf("Expected error fetching deleted security") } if herr, ok := err.(*handlers.Error); ok { - if herr.ErrorId != 3 { + if herr.ErrorId != 3 { // Invalid requeset t.Fatalf("Unexpected API error fetching deleted security: %s", herr) } } else { diff --git a/internal/handlers/sessions_test.go b/internal/handlers/sessions_test.go index 0bc0b03..65a079f 100644 --- a/internal/handlers/sessions_test.go +++ b/internal/handlers/sessions_test.go @@ -27,8 +27,12 @@ func newSession(user *User) (*http.Client, error) { func getSession(client *http.Client) (*handlers.Session, error) { var s handlers.Session - read(client, &s, "/session/", "session") - return &s, nil + err := read(client, &s, "/session/", "session") + return &s, err +} + +func deleteSession(client *http.Client) error { + return remove(client, "/session/", "session") } func sessionExistsOrError(c *http.Client) error { @@ -79,3 +83,37 @@ func TestGetSession(t *testing.T) { } }) } + +func TestDeleteSession(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + err := deleteSession(d.clients[0]) + if err != nil { + t.Fatalf("Unexpected error removing session: %s\n", err) + } + err = deleteSession(d.clients[0]) + if err != nil { + t.Fatalf("Unexpected error attempting to delete nonexistent session: %s\n", err) + } + _, err = getSession(d.clients[0]) + if err == nil { + t.Fatalf("Expected error fetching deleted session") + } + if herr, ok := err.(*handlers.Error); ok { + if herr.ErrorId != 1 { // Not Signed in + t.Fatalf("Unexpected API error fetching deleted session: %s", herr) + } + } else { + t.Fatalf("Unexpected error fetching deleted session") + } + + // Login again so we don't screw up the TestData teardown code + userWithPassword := d.users[0] + userWithPassword.Password = data[0].users[0].Password + + client, err := newSession(&userWithPassword) + if err != nil { + t.Fatalf("Unexpected error re-creating session: %s\n", err) + } + d.clients[0] = client + }) +}