github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/web/manifest/manifest_internal_test.go (about)

     1  package manifest
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestForm(t *testing.T) {
    11  	tests := []struct {
    12  		name       string
    13  		domain     string
    14  		status     int
    15  		hasWebhook bool
    16  	}{
    17  		{
    18  			name:       "no_webhook",
    19  			domain:     "localhost",
    20  			status:     http.StatusOK,
    21  			hasWebhook: false,
    22  		},
    23  		{
    24  			name:       "webhook",
    25  			domain:     "example.com",
    26  			status:     http.StatusOK,
    27  			hasWebhook: true,
    28  		},
    29  	}
    30  	for _, tt := range tests {
    31  		t.Run(tt.name, func(t *testing.T) {
    32  			rr := httptest.NewRecorder()
    33  			if err := form(rr, tt.domain); err != nil {
    34  				t.Fatalf("form() failed with error: %v", err)
    35  			}
    36  			if status := rr.Code; status != tt.status {
    37  				t.Fatalf("form() returned wrong status code: got %v want %v", status, tt.status)
    38  			}
    39  			body := rr.Body.String()
    40  			if tt.hasWebhook {
    41  				if !strings.Contains(body, "default_events") {
    42  					t.Errorf("form() returned body without default_events")
    43  				}
    44  				if !strings.Contains(body, "hook_attributes") {
    45  					t.Errorf("form() returned body without hook_attributes")
    46  				}
    47  				if !strings.Contains(body, `"active": true`) {
    48  					t.Errorf("form() returned body without active webhook")
    49  				}
    50  			} else {
    51  				if strings.Contains(body, "default_events") {
    52  					t.Errorf("form() returned body with default_events")
    53  				}
    54  				if strings.Contains(body, "hook_attributes") {
    55  					t.Errorf("form() returned body with hook_attributes")
    56  				}
    57  				if strings.Contains(body, `"active": true`) {
    58  					t.Errorf("form() returned body with active webhook")
    59  				}
    60  			}
    61  			if t.Failed() {
    62  				t.Log(body)
    63  			}
    64  		})
    65  	}
    66  }