💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › a8ded842d7f08c4259d23785da… captured on 2023-09-08 at 16:28:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-05-24)

🚧 View Differences

-=-=-=-=-=-=-

Go Back

0 package db

1

2 import (

3 "testing"

4 "time"

5 "gemigit/test"

6 )

7

8 func TestCreateToken(t *testing.T) {

9

10 initDB(t)

11

12 user, _ := createUserAndSession(t)

13

14 _, err := user.CreateToken()

15 test.IsNil(t, err)

16 }

17

18 func TestRenewToken(t *testing.T) {

19

20 initDB(t)

21

22 user, _ := createUserAndSession(t)

23

24 test.IsNotNil(t, user.RenewToken(-1), "token id should be invalid")

25

26 _, err := user.CreateToken()

27 test.IsNil(t, err)

28

29 tokens, err := user.GetTokens()

30 test.IsNil(t, err)

31

32 test.IsEqual(t, len(tokens), 1)

33

34 test.IsNil(t, user.RenewToken(tokens[0].ID))

35 }

36

37 func TestDeleteToken(t *testing.T) {

38

39 initDB(t)

40

41 user, _ := createUserAndSession(t)

42

43 test.IsNotNil(t, user.DeleteToken(-1), "token id should be invalid")

44

45 _, err := user.CreateToken()

46 test.IsNil(t, err)

47

48 tokens, err := user.GetTokens()

49 test.IsNil(t, err)

50 test.IsEqual(t, len(tokens), 1)

51

52 test.IsNil(t, user.DeleteToken(tokens[0].ID))

53 }

54

55 func TestGetTokens(t *testing.T) {

56

57 initDB(t)

58

59 user, _ := createUserAndSession(t)

60

61 user.GetTokens()

62

63 tokens, err := user.GetTokens()

64 test.IsNil(t, err)

65 test.IsEqual(t, len(tokens), 0)

66

67 first, err := user.CreateToken()

68 test.IsNil(t, err)

69

70 second, err := user.CreateToken()

71 test.IsNil(t, err)

72

73 tokens, err = user.GetTokens()

74 test.IsNil(t, err)

75 test.IsEqual(t, len(tokens), 2)

76

77 test.IsEqual(t, tokens[0].Hint, first[0:4])

78 test.IsEqual(t, tokens[1].Hint, second[0:4])

79

80 test.IsNil(t, user.DeleteToken(tokens[0].ID))

81

82 tokens, err = user.GetTokens()

83 test.IsNil(t, err)

84 test.IsEqual(t, len(tokens), 1)

85 test.IsEqual(t, tokens[0].Hint, second[0:4])

86 }

87

88 func TestCanUsePassword(t *testing.T) {

89

90 initDB(t)

91

92 _, err := CanUsePassword("invalid", "invalid", "invalid")

93 test.IsNotNil(t, err, "should return user not found")

94

95 user, signature := createUserAndSession(t)

96 repo := test.FuncName(t)

97

98 _, err = CanUsePassword("invalid", user.Name, user.Name)

99 test.IsNotNil(t, err, "should return repository not found")

100

101 test.IsNil(t, user.CreateRepo(repo, signature))

102

103 b, err := CanUsePassword(repo, user.Name, user.Name)

104 test.IsNil(t, err)

105 test.IsEqual(t, b, true)

106

107 test.IsNil(t, user.ToggleSecure())

108

109 b, err = CanUsePassword(repo, user.Name, user.Name)

110 test.IsNil(t, err)

111 test.IsEqual(t, b, false)

112

113 test.IsNil(t, user.ToggleSecure())

114

115 b, err = CanUsePassword(repo, user.Name, user.Name)

116 test.IsNil(t, err)

117 test.IsEqual(t, b, true)

118 }

119

120 func TestTokenAuth(t *testing.T) {

121

122 initDB(t)

123

124 user, _ := createUserAndSession(t)

125

126 test.IsNotNil(t, TokenAuth(user.Name, "invalid"),

127 "token should be invalid")

128

129 token, err := user.CreateToken()

130 test.IsNil(t, err)

131 test.IsNil(t, TokenAuth(user.Name, token))

132

133 tokens, err := user.GetTokens()

134 test.IsNil(t, err)

135 test.IsEqual(t, len(tokens), 1)

136

137 _, err = db.Exec("UPDATE token SET expiration = ? WHERE tokenID = ?",

138 time.Now().Unix() - 1, tokens[0].ID)

139 test.IsNil(t, err)

140 test.IsNotNil(t, TokenAuth(user.Name, token),

141 "token should be expired")

142 }

143