• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SyTW2223 / E01 / 3829413404

pending completion
3829413404

push

github

Nicolas Vegas
Test de get user.

22 of 42 branches covered (52.38%)

Branch coverage included in aggregate %.

16 of 17 new or added lines in 5 files covered. (94.12%)

1 existing line in 1 file now uncovered.

223 of 293 relevant lines covered (76.11%)

2.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.44
/server/src/controllers/userController.ts
1
import { generateToken } from "../authentication/token";
4✔
2
import { Objective } from "../models/objective";
4✔
3
import { Session } from "../models/session";
4✔
4
import { Task } from "../models/task";
4✔
5
import { User } from "../models/user";
4✔
6

7
const bcrypt = require('bcrypt');
4✔
8

9
/**
10
 * @method registerUser is a function used to register a new user in the application.
11
 * @param {any} req - The request object, which should contain a body with the following fields:
12
 *   - name: the name of the user to register (string)
13
 *   - password: the password of the user to register (string)
14
 * @param {any} res - The response object, which will be used to send the response to the client.
15
 * @router : /user
16
 * @returns {void} On success, a status code of 201 will be sent. If the user already exists, 
17
 * a status code of 404 with an error message will be sent. On error, a status code of 500 and the error will be sent.
18
 */
19
export const registerUser = async (req: any, res: any) => {
4✔
20
  try {
3✔
21
    const userExists = await User.findOne({ name: req.body.name });
3✔
22
    if (userExists) {
3✔
23
      res.status(404).json({ message: "User alredy exist", status: 404 });
1✔
24
    } else {
25
      const salt = await bcrypt.genSalt(10)
2✔
26
      const hashedPassword = await bcrypt.hash(req.body.password, salt) 
2✔
27
      const user = new User({
1✔
28
        name: req.body.name,
29
        password: hashedPassword,
30
        sessions: [],
31
        token: generateToken(req.body.password) as string,
32
      });
33
      await user.save();
1✔
34
      res.status(201).send();
1✔
35
    }
36
  } catch (error) {
37
    res.status(500).send(error);
1✔
38
  }
39
}
40
/**
41
 * @method loginUser is a function used to log in a user to the application.
42
 * 
43
 * @param {any} req - The request object, which should contain a body with the following fields:
44
 *   - name: the name of the user logging in (string)
45
 *   - password: the password of the user logging in (string)
46
 * @param {any} res - The response object, which will be used to send the response to the client.
47
 * 
48
 * @router : /user/login
49
 * 
50
 * @returns {void} On success, a status code of 200 will be sent. If the user does not exist, a status code of 404 will be sent. If the password is incorrect, a status code of 400 will be sent. On error, a status code of 500 and the error will be sent.
51
 */
52
export const loginUser = async (req: any, res: any) => {
4✔
53
  try {
3✔
54
    const userExists = await User.findOne({ name: req.body.name });
3✔
55
    if (userExists) {
3✔
56
      await bcrypt.compare(req.body.password, userExists.password) ? res.status(200).send({token: generateToken(userExists._id.toString())}) : res.status(400).send();
2✔
57
    } else {
58
      res.status(404).send();
1✔
59
    }
60
  } catch (error) {
61
    res.status(500).send(error);
×
62
  }
63
}
64

65
/**
66
 * @method getUser is a function used to retrieve a user from the application.
67
 * 
68
 * @param {any} req - The request object, which should contain a query string with the following field:
69
 *   - name: the name of the user to retrieve (string).
70
 * @param {any} res - The response object, which will be used to send the response to the client.
71
 * 
72
 * * @router : /user
73
 * 
74
 * @returns {void} On success, a status code of 200 and the user(s) will be sent. If no user is found, a 
75
 * status code of 404 will be sent. On error, a status code of 500 will be sent.
76
 */
77
export const getUser = async (req: any, res: any) => {
4✔
78
  const filter = { name: req.query.name };
1✔
79
  User.findOne(filter).then((user) => {
1✔
80
    if (user) {
1!
81
      res.status(200).send(user);
1✔
82
    } else {
UNCOV
83
      res.status(404).send();
×
84
    }
85
  }).catch(() => {
86
    res.status(500).send();
×
87
  });
88
}
89

90
/**
91
 * @method deleteUser is a function used to delete a user and all of their associated sessions, objectives, and tasks from the application.
92
 * 
93
 * @param {Request} req - The request object, which should contain a query string with the following field:
94
 *   - name: the name of the user to delete (string)
95
 * @param {Response} res - The response object, which will be used to send the response to the client.
96
 * 
97
 * @route: /user
98
 * 
99
 * @returns {void} On success, a status code of 200 and a message will be sent. If no user is found, a status code of 404 and a message will be sent. On error, a status code of 500 and an error message will be sent.
100
 */
101
export const deleteUser = async (req: any, res: any) => {
4✔
102
  User.findOneAndDelete({ name: req.query.name as string })
2✔
103
  .then((result) => {
104
    if (result) {
2✔
105
      result.sessions.forEach((session) => {
1✔
106
        let filter = session.toString(); 
×
107
        Session.findByIdAndDelete(filter as string)
×
108
        .then((each_session) => {
109
          each_session?.objectives.forEach((objective) => {
×
110
            let filter = objective.toString(); 
×
111
            Objective.findByIdAndDelete(filter as string)
×
112
            .then((each_objectives) => {
113
              each_objectives?.tasks.forEach((task) => {
×
114
                let filter = task.toString(); 
×
115
                Task.findByIdAndDelete(filter as string)
×
116
                .then(() => {
117
                  res.status(200);
×
118
                });
119
              });
120
            });
121
          });
122
        });
123
      });
124
      res.status(200).send("User, User's session, objectives and tasks deleted.");
1✔
125
    } else {
126
      res.status(404).json({ message: "User not Found" });
1✔
127
    }
128
  })
129
  .catch((err) => {
130
    res.status(500).json({ error: err });
×
131
  });
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc