github.com/knoebber/dotfile@v1.0.6/local/integration_test.go (about)

     1  package local
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/knoebber/dotfile/db"
     6  	"github.com/knoebber/dotfile/dotfileclient"
     7  	"github.com/knoebber/dotfile/server"
     8  	"github.com/stretchr/testify/assert"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  const (
    14  	dotfilehubAddr     = ":26900"
    15  	dotfilehubUsername = "user"
    16  	dotfilehubPassword = "password12345"
    17  )
    18  
    19  func failIf(t *testing.T, err error, context ...string) {
    20  	if err != nil {
    21  		t.Fatalf("%s %v", err, context)
    22  	}
    23  }
    24  
    25  func TestDotfilehubIntegration(t *testing.T) {
    26  	failIf(t, os.Chdir("../server"), "changing directory so that assets work in integration test")
    27  
    28  	setupTestFile(t)
    29  	dotfilehub, err := server.New(server.Config{
    30  		Addr: dotfilehubAddr,
    31  	})
    32  	failIf(t, err)
    33  
    34  	defer dotfilehub.Close()
    35  
    36  	go func() {
    37  		if err := dotfilehub.ListenAndServe(); err != nil {
    38  			// Expected after close.
    39  			fmt.Printf("dotfilehub listen and serve: %s\n", err)
    40  		}
    41  	}()
    42  
    43  	user, err := db.CreateUser(db.Connection, dotfilehubUsername, "", dotfilehubPassword)
    44  	failIf(t, err)
    45  
    46  	client := dotfileclient.New("http://"+dotfilehubAddr, dotfilehubUsername, user.CLIToken)
    47  
    48  	s := testStorage()
    49  	failIf(t, s.SetTrackingData())
    50  	failIf(t, s.Push(client))
    51  
    52  	convertedPath, err := convertPath(testTrackedFile)
    53  	failIf(t, err, "converting path")
    54  
    55  	temp := &db.TempFileRecord{
    56  		UserID:  user.ID,
    57  		Content: []byte(testUpdatedContent),
    58  		Alias:   testAlias,
    59  		Path:    convertedPath,
    60  	}
    61  	failIf(t, temp.Create(db.Connection), "creating temp file")
    62  	failIf(t, db.InitOrCommit(user.ID, testAlias, "new content on server"), "committing to file on server")
    63  
    64  	failIf(t, s.Pull(client))
    65  	content, err := s.DirtyContent()
    66  	failIf(t, err)
    67  
    68  	assert.Equal(t, testUpdatedContent, string(content))
    69  }